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

时间:2014-04-10 05:51:10

标签: java android android-asynctask nullpointerexception

d.android.com for onPreExecute()中声明它在doInBackground(Params...)之前在UI线程上运行,因此它应该可以轻松地从TextView访问setText()并执行Activity方法它来自executed()

但是在下面的代码中,loading TextView 在类SplashScreen内私有声明,扩展了活动。在onCreate()内部,它与UI的TextView小部件链接。但是当AsyncTask扩展类Atom时,执行函数onPreExecute()会为其中执行的语句NullPointerExcepction抛出loading.setText("Loading...");

这里是代码

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());

        }


        }
 }

筹码:

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)

3 个答案:

答案 0 :(得分:3)

尝试在执行TextView之前初始化asyntask。喜欢以下。

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();

}

我不知道这是否正确,这是我的猜测,所以,请让我知道发生了什么。

由于

答案 1 :(得分:3)

在调用AsyncTask之前初始化文本视图。做这样的事

 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();

        }

答案 2 :(得分:2)

在调用asynctask之前,必须先初始化textview。将您的代码更改为以下内容 -

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();

            }
 }