简体   繁体   English

Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序

[英]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.

相关问题 Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android Android无法在未调用looper.prepare()的线程内创建处理程序 - Android can't create handler inside thread that has not called looper.prepare() Android AsyncTask [无法在未调用Looper.prepare()的线程内创建处理程序 - Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()] java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android Android致命异常:无法在未调用Looper.prepare()的线程内创建处理程序 - Android fatal exception: Can't create handler inside thread that has not called Looper.prepare() Android等待进度条无法在未调用Looper.prepare()错误的线程内创建处理程序 - Android Waiting progress bar Can't create handler inside thread that has not called Looper.prepare() error 警报对话框崩溃Android测试 - “无法在未调用Looper.prepare()的线程内创建处理程序” - Alert Dialog crashes Android test - “Can't create handler inside thread that has not called Looper.prepare()” Android上的Junit测试领域。 无法在未调用Looper.prepare()的线程内创建处理程序 - Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM