安卓 java 开发 解决方法: java.lang.RuntimeException: Can’t create handler inside thread that has not call

java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()

这时今天遇到的第二个错误。

注意:一定要在程序开头导入包:

import android.os.Looper;

 

原因是非主线程中没有创建Looper对象,需要先调用Looper.prepare()启用Looper。

解决办法是在线程的开始跟结束间调用Looper.prepare(); 跟Looper.loop();

如:

ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

cachedThreadPool.execute(new Runnable() {

@Override

public void run() {

Looper.prepare(); 

***********

Looper.loop();

}

});

注意:一个线程只能有一个Looper


关注公众号,了解更多it技术(it问答网

发表评论

电子邮件地址不会被公开。