Note that there are some explanatory texts on larger screens.

plurals
  1. POcant call function from inside thread or asyntask
    primarykey
    data
    text
    <p>I am using a thread and a handler in android. The app works fine as long as i dont any function from outside the activity. But if i call some funcyion from outside the activity from inside a thread, it gives NullPointerException.</p> <pre><code>package com.prog; import com.API.TextBoxCheck; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class ProgressBarExample extends Activity { private Handler handler = new Handler(); int i; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ TextView tv=(TextView)findViewById(R.id.textView1); @Override public void onClick(View v) { // TODO Auto-generated method stub Thread thread = new Thread(null, doBackgroundThreadProcessing, "Background"); thread.start(); }}); Button stopBtn=(Button)findViewById(R.id.button2); stopBtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); }}); } private Runnable doBackgroundThreadProcessing = new Runnable() { public void run() { try { backgroundThreadProcessing(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; private void backgroundThreadProcessing() throws InterruptedException { TextView tv=(TextView)findViewById(R.id.textView1); i=0; while(i&lt;100) { handler.post(doUpdateGUI); Thread.sleep(50); i++; } EditText et=(EditText)findViewById(R.id.editText1); TextBoxCheck tbc = new com.API.TextBoxCheck(); String reply=tbc.TextBoxChecker(et.getText().toString(),10); Log.d("thread", reply); } private Runnable doUpdateGUI = new Runnable() { public void run() { updateGUI(); } private void updateGUI() { // TODO Auto-generated method stub TextView tv=(TextView)findViewById(R.id.textView1); tv.setText(i+"%"); } }; } </code></pre> <p>I have left out the code of textBoxCheck becoz i think it may be unnecesarry here. Please help me on this.</p> <p>PS. : I also tried using AsyncTask but the same problem occurs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload