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 classAtom
时,函数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();
}