![](/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.