Can't create handler inside thread that has not called Looper.prepare()问题解决

时间:2022-04-07 20:44:39

写在前面:

我们都知道,UI的显示要在UI线程中执行,而不能在子线程中,也不能在异步线程中执行。可是,有时候,不知不觉就会违反这一规则,下面,就是一例,亲身经历。


1. 首先,看一下错误信息:

 W/dalvikvm(25729): threadid=14: thread exiting with uncaught exception (group=0x413e7360)
: E/AndroidRuntime(25729): FATAL EXCEPTION: AsyncTask #2
: E/AndroidRuntime(25729): java.lang.RuntimeException: An error occured while executing doInBackground()
 E/AndroidRuntime(25729): at android.os.AsyncTask$3.done(AsyncTask.java:299)
 E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
: E/AndroidRuntime(25729): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
: E/AndroidRuntime(25729): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
: E/AndroidRuntime(25729): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
: E/AndroidRuntime(25729): at java.lang.Thread.run(Thread.java:856)
: E/AndroidRuntime(25729): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
: E/AndroidRuntime(25729): at android.os.Handler.<init>(Handler.java:121)
: E/AndroidRuntime(25729): at android.widget.Toast$TN.<init>(Toast.java:335)
: E/AndroidRuntime(25729): at android.widget.Toast.<init>(Toast.java:101)
: E/AndroidRuntime(25729): at android.widget.Toast.makeText(Toast.java:248)
......
: E/AndroidRuntime(25729): at android.os.AsyncTask$2.call(AsyncTask.java:287)
: E/AndroidRuntime(25729): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
: E/AndroidRuntime(25729): ... 5 more


2. 分析:


很明显,是由于AsyncTask和handler运用的不正确。大多是由于在异步线程中调用了UI相关函数。


3. 具体到本次错误的原因,是因为在异步线程中调用了Toast来。代码片段如下:


private MyAsyncTask mMyAsyncTask;


private MyAsyncTask extends AsyncTask.... {
...
@Override
protected void onPostExecute(String result) {
if (result.equals("0")) {

mMyTestListener.onEvent("OK");   //通过接口的方式将异步线程的执行结果传到调用层。
}else {
mMyTestListener.onEvent(null);
}


}


private MyHandler mMyHandler;
private class MyHandler extends Handler {

public void start() {
sendEmptyMessage(0);
}

@Override
public void handleMessage(Message msg) {
Log.i(TAG, "handleMessage msg ===" + msg);
Object obj = msg.getData();
if (msg.what == 0) {
doSomething();
}else {

Toast.makeText(mContext, ""+ obj, Toast.LENGTH_LONG).show();  //修改后的
}

}
};






/**
* 用于更新ui
*/
private MyTestListener mMyTestListener = new MyTestListener();


class MyTestListener implements MyBaseListener {



@Override
public void onEvent(String res) {

Message msg = new Message();
msg.what = 1;
msg.obj = res;

if (res!=null) {
   doSomething();
}else {
   Toast.makeText(mContext, ""+ obj, Toast.LENGTH_LONG).show();  //修改前的,error
}

}

}

4. 总结:

   1.  将Toast放在handler中显示。

   2. 通过interface在异步线程中调用它的函数,这个interface的实现一定不能进行UI的操作。