简体   繁体   中英

NullPointerExcepction for TextView.setText() when called inside onPreExecute()

It is stated in d.android.com for onPreExecute() that it runs on the UI thread before doInBackground(Params...) so it should easily access the TextView and perform setText() method from the Activity from which it was executed() .

But here in the below codes the loading TextView is privately declared inside the class SplashScreen that extends Activity . Inside the onCreate() it is linked with the TextView widget of the UI. But when AsyncTask extended class Atom the function onPreExecute() is executed which throws a NullPointerExcepction for the statement loading.setText("Loading..."); executed inside it.

Here the code

public class SplashScreen extends Activity implements AnimationListener{
...
TextView loading=null;
...

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);


            try {
                a = (Atom) new Atom().execute(null,null,null);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                finish();

            }
...

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

 ....

 }

 public class Atom extends AsyncTask<RSSFeed, Void, RSSFeed>{

    private RSSReader reader;
    private RSSFeed feed = null;
    private String uri = "http://website.com/feed/";

    @Override
    protected void onPreExecute() {

       super.onPreExecute();
      //------------problem----area-------------------
       loading.setText("Loading...");
      //------------problem----area-------------------  


    }

        @Override
        protected RSSFeed doInBackground(RSSFeed... arg0) {


            reader = new RSSReader();

              try {
                feed = reader.load(uri);
                Log.d("rss", feed.getTitle());



            } catch (RSSReaderException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

              return feed;
        }



        @Override
        protected void onPostExecute(RSSFeed result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            prg.cancel();

            t(result.getTitle().toString());

        }


        }
 }

The stack:

03-09 10:50:12.793: W/System.err(14214): java.lang.NullPointerException
03-09 10:50:12.813: W/System.err(14214):    at in.edu.ss.er.splash.SplashScreen$Atom.onPreExecute(SplashScreen.java:158)
03-09 10:50:12.827: W/System.err(14214):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-09 10:50:12.833: W/System.err(14214):    at android.os.AsyncTask.execute(AsyncTask.java:534)
03-09 10:50:12.833: W/System.err(14214):    at in.edu.ss.er.splash.SplashScreen.onCreate(SplashScreen.java:45)

Try to initialize TextView before executing asyntask . Like following.

try {
    loading = (TextView) findViewById(R.id.textView2);
    a = (Atom) new Atom().execute(null,null,null);
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    finish();

}

I don't know this is correct or not, This my guessing, So, Please let me know what happened.

Thanks

just initialize your text view before calling AsyncTask. do something like this

 loading = (TextView) findViewById(R.id.textView2);
 try {
            a = (Atom) new Atom().execute(null,null,null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            finish();

        }

You have to initialize your textview before then call asynctask. Change your code into following-

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    loading = (TextView) findViewById(R.id.textView2);

            try {
                a = (Atom) new Atom().execute(null,null,null);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                finish();

            }
 }

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