[英]Android handler: Can't create handler inside thread that has not called Looper.prepare()
I have a thread: 我有一个线程:
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);
// ....
}
}
This thread "KelvinThread" is created and started from another Thread. 该线程“ KelvinThread”是从另一个线程创建并启动的。
I have this error: 我有这个错误:
Can't create handler inside thread that has not called Looper.prepare()
无法在未调用Looper.prepare()的线程内创建处理程序
when my handler is allocated : mSoundHandler = new Handler(); 当分配我的处理程序时:mSoundHandler = new Handler();
Thank you for your help guys ! 谢谢您的帮助!
Please refer to Looper documentation . 请参考Looper文档 。 There is also example.
也有例子。
UPD You might also be able to use Looper.getMainLooper() to attach handler to main looper instead of creating thread one. UPD您也可以使用Looper.getMainLooper()将处理程序附加到主循环程序,而不用创建线程一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.