简体   繁体   English

在onPreExecute()内部调用时,TextView.setText()的NullPointerExcepction

[英]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() . d.android.com中声明onPreExecute()它在doInBackground(Params...)之前在UI线程上运行,因此它应该可以轻松访问TextView并从executed()它的Activity执行setText()方法executed()

But here in the below codes the loading TextView is privately declared inside the class SplashScreen that extends Activity . 但是在下面的代码中, loading TextView是在SplashScreen类中私有声明的,它扩展了Activity Inside the onCreate() it is linked with the TextView widget of the UI. onCreate()内部,它与UI的TextView小部件链接。 But when AsyncTask extended class Atom the function onPreExecute() is executed which throws a NullPointerExcepction for the statement loading.setText("Loading..."); 但是当AsyncTask扩展类Atom ,执行onPreExecute()函数,它会为语句loading.setText("Loading...");抛出NullPointerExcepction 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 . 尝试在执行asyntask之前初始化TextView 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. 只需在调用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. 您必须先初始化textview,然后再调用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();

            }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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