我在使用其处理程序线程实例退出线程时获得空指针异常,实例值在创建后变为null。如果我将变量设为静态,那么我没有遇到这个问题。任何人都可以向我解释为什么Handler线程实例在创建后变为null,下面的代码将简要解释我的问题。
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.
}
}
}