简体   繁体   中英

TextView giving NulllPointerException in AsyncTask

that should be a trivial , but i am really wondering whats happening, as i copied the code from an online course, where it worked , and its giving me nullpointer exception.Everything seems right so far

here is the MainActivity class

public class MainActivity extends Activity {


Button button1;
TextView textview2;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    TextView textview2=(TextView)findViewById(R.id.textview2);

    Button button1=(Button)findViewById(R.id.button1);



    ProgressBar progressbar1=(ProgressBar)findViewById(R.id.progressbar1);

        button1.setOnClickListener(new Button.OnClickListener() {


          public void onClick(View v) {



            asy task=new asy(MainActivity.this);
            task.execute("Process me","Process me too");





            }


        });
}

}

then thats the sniplet from the Async

public class asy extends AsyncTask<String,Integer,Long> {


public MainActivity host;

public asy (MainActivity host) {
    this.host=host;

}
@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub



    super.onPreExecute();
  host.textview2.setText("Processing..");

;
}

and in logcat i see

E/AndroidRuntime(28044): FATAL EXCEPTION: main
E/AndroidRuntime(28044): java.lang.NullPointerException
E/AndroidRuntime(28044):    at com.example.async.asy.onPreExecute(asy.java:25)
E/AndroidRuntime(28044):    at android.os.AsyncTask.execute(AsyncTask.java:391)

i mean everything looks right to me , i initialize TextView after SetContentView, i use constructors, why is it not initialized in the AsyncTask ?

public class MainActivity extends Activity 
{
Button button1;
TextView textview2;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);

    textview2=(TextView)findViewById(R.id.textView2);

    button1=(Button)findViewById(R.id.button1);



    progressbar=(ProgressBar)findViewById(R.id.progressbar1);

        button1.setOnClickListener(new Button.OnClickListener()
        {
          public void onClick(View v) 
          {
            asy task=new asy(MainActivity.this);
            task.execute("Process me","Process me too");
          }
        });
}

public class asy extends AsyncTask<String,Integer,Long> 
{

    public MainActivity host = new MainActivity();

    public asy (MainActivity host) 
    {
        this.host=host;
    }
    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        host.textview2.setText("Processing..");
    }
    @Override
    protected Long doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

}

Assuming the host passed to task's constructor is MainActivity object. Then in main activity you are not assigning the TextView to instance variables:

These are still null:

Button button1;
TextView textview2;
ProgressBar progressbar;

So, this in onCreate() :

TextView textview2=(TextView)findViewById(R.id.textview2);
Button button1=(Button)findViewById(R.id.button1);
ProgressBar progressbar1=(ProgressBar)findViewById(R.id.progressbar1);

should be changed to:

textview2=(TextView)findViewById(R.id.textview2);
button1=(Button)findViewById(R.id.button1);
progressbar=(ProgressBar)findViewById(R.id.progressbar1);

Declare your TextView instance as global and use it in AsyncTask.

Like..

TextView textview2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

        textview2=(TextView)findViewById(R.id.textview2);

And use it as :

textview2.setText("Processing..");

Remove TextView from TextView textview2 = (TextView) findViewById(R.id.textview2);

you have already declared it above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM