Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在onPreExecute()内调用TextView.setText()时的NullPointerException_Java_Android_Android Asynctask_Nullpointerexception - Fatal编程技术网

Java 在onPreExecute()内调用TextView.setText()时的NullPointerException

Java 在onPreExecute()内调用TextView.setText()时的NullPointerException,java,android,android-asynctask,nullpointerexception,Java,Android,Android Asynctask,Nullpointerexception,它在doInBackground(Params…之前在UI线程上运行,因此它应该可以轻松访问TextView并从执行它的活动中执行setText()方法 但是在下面的代码中,加载文本视图是在扩展活动的类SplashScreen中私下声明的。在onCreate()内部,它与UI的TextView小部件链接。但是当执行AsyncTaskextended classAtom时,函数onPreExecute()会为语句loading.setText(“loading…”)抛出一个NullPointerE

它在
doInBackground(Params…
之前在UI线程上运行,因此它应该可以轻松访问
TextView
并从执行它的
活动中执行
setText()
方法

但是在下面的代码中,
加载
文本视图是在扩展活动的类
SplashScreen
中私下声明的。在
onCreate()
内部,它与UI的TextView小部件链接。但是当执行
AsyncTask
extended class
Atom
时,函数
onPreExecute()
会为语句
loading.setText(“loading…”)抛出一个
NullPointerException
在其内部执行

这是密码

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

        }


        }
 }

在执行
asyntask
之前,尝试初始化
TextView
。像下面这样

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

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


谢谢

在调用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();

            }
 }

只需在调用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();

        }

你确定你的xml中有一个id为
R.id.textView2
的TextView吗?你在哪里调用这个AyncTask?请同时添加你的xml代码。在主UI线程SplashScreen(编辑了这个问题!)中,我想你在声明TextView之前调用的是异步任务。因此它会出现空指针异常。
 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();

        }