![](/img/trans.png)
[英]Android thread error - Can't create handler inside thread that has not called Looper.prepare()
[英]Android handler: Can't create handler inside thread that has not called Looper.prepare()
我有一个线程:
public class KelvinThread implements Runnable {
private Handler mSoundHandler;
public KelvinThread() {
super();
mSoundHandler = new Handler();
}
private Runnable mSoundThread = new Runnable() {
public void run() {
// my treatment
}
};
@Override
public void run() {
while (true) {
// ....
mSoundHandler.postDelayed(mSoundThread, 2000);
// ....
}
}
该线程“ KelvinThread”是从另一个线程创建并启动的。
我有这个错误:
无法在未调用Looper.prepare()的线程内创建处理程序
当分配我的处理程序时:mSoundHandler = new Handler();
谢谢您的帮助!
请参考Looper文档 。 也有例子。
UPD您也可以使用Looper.getMainLooper()将处理程序附加到主循环程序,而不用创建线程一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.