我正面临关于处理程序的空指针异常。令人费解的是,我在调试/单步执行代码时却没有得到它,而是在执行时。
我有一个这样的课程
public class MyActivity extends Activity{
public onCreate(){
...
}
public class MessageHandlerCallback implements Handler.Callback {
public void setHandler(Handler handler, Looper looper){
this.handler = handler;
this.looper = looper;
}
public boolean handleMessage(Message msg) {
...
}
}
protected class BackgroundPopulateThread extends Thread {
public bthandler;
private MessageHandlerCallback handlercb = null;
public void run(){
Looper.prepare();
handlercb = new MessageHandlerCallback();
btHandler = new Handler(handlercb);
handlercb.setHandler(btHandler, Looper.myLooper());
Looper.loop();
}
public Handler getHandler(){
return btHandler;
}
}
protected class InitTask extends AsyncTask<Context, Integer, Integer> {
protected Integer doInBackground(Context... params) {
...
}
protected void onPostExecute(Integer result)
{
...
BackgroundPopulateThread bt = new BackgroundPopulateThread();
bt.start();
Handler handler = bt.getHandler();
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg); <<<< Null pointer exception here.
}
} // end of InitTask
} // end of MyActivity
我正在使用AsyncTask类onPostExecute()方法创建上述后台线程,如下所示:
当我在后台线程构造函数中创建处理程序和c / b对象的实例时,我得到了这个错误,但又遇到了另一个问题,即android.os.NetworkIOonMainThread Exception。
任何评论都会非常感谢!!!
谢谢! SWZ