我在使用其处理程序线程实例退出线程时遇到空指针异常,实例值在创建后变为空。如果我将变量设为静态,那么我就不会遇到这个问题。谁能解释一下为什么 Handler 线程实例在创建后变为空,下面的代码将简要解释我的问题。
private class MyHandler extends Handler{
private HandlerThread hThread;
private MyHandler(Looper looper){
super(looper);
}
public MyHandler(){
//Created my handler thread instance here
hThread = new HandlerThread(... , Process.THREAD_PRIORITY_BACKGROUND);
hThrad.start();
new MyHandler(hThread.getLooper);
}
这是我的处理消息方法:
public void HandlerMessage(Message msg){
super(msg);
try{
/*Here I am executing some utility methods*/
}catch(Exception exce){
}finally{
/*While reading the instance of Handler thread it says null*/
hThread.quit();//Throws null pointer exception.
}
}
}