简体   繁体   English

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

[英]Android thread error - Can't create handler inside thread that has not called Looper.prepare()

I am trying to get a timer to run in a separate thread. 我正在尝试让计时器在单独的线程中运行。

I have the following declaration before my onCreate function: 我的onCreate函数之前有以下声明:

TimerTask scanTask;
Timer t = new Timer();

Then the following code within onCreate: 然后是onCreate中的以下代码:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        scanTask = new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer test");
            }
        };

        t.schedule(scanTask, 0, 5000);

        CountDownTimer waitTimer;
        waitTimer = new CountDownTimer(20000,300) {
            @Override
            public void onTick(long l) {
            }
            @Override
            public void onFinish() {
                t.cancel();
                System.out.println("Timer stopped");
            }
        }.start();
    }
}; 

Thread periodic_scan = new Thread(runnable);
periodic_scan.start();

However, when I run the app, it crashes and gives me the error: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 但是,当我运行该应用程序时,它崩溃并给了我错误: java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

I'm not sure I completely understand why this error is occurring. 我不确定我是否完全理解为什么会发生此错误。 Is it something to do with the UI thread? 与UI线程有关吗? Also, I'm not sure whether the way I've tried to implement this is correct. 另外,我不确定我尝试实现此方法的方式是否正确。 This is my first time trying to deal with threads in Android. 这是我第一次尝试处理Android中的线程。

you can use HandlerThread like 你可以像这样使用HandlerThread

HandlerThread handlerThread = new HandlerThread("name");

handlerThread.start();

Handler threadHandler = new Handler(handlerThread.getLooper(),new Callback() {
    public boolean handleMessage(Message msg) {

    return true;  
 }
});

I ended up changing the code a bit and decided to use a Thread class: 我最终对代码进行了一些更改,并决定使用Thread类:

class TimerThread extends Thread {

    TimerTask scanTask;
    Timer t = new Timer();

    @Override
    public void run() {
        Looper.prepare();
        scanTask = new TimerTask() {
            public void run() {
                System.out.println("timer test");
            }
        };

        t.schedule(scanTask, 0, 5000);

        CountDownTimer waitTimer;
        waitTimer = new CountDownTimer(20000,300) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                t.cancel();
                System.out.println("Timer stopped");
            }
        }.start();

        Looper.loop();
    }
}

In onCreate I used the following: onCreate我使用了以下内容:

new TimerThread().start();

The program now works without any errors, however the only problem now is that there is a noticeable 2-3 second lag when the program loads up before the UI renders to the screen. 该程序现在可以正常运行了,但是唯一的问题是,在UI呈现到屏幕之前加载该程序时,会有明显的2-3秒的延迟。

I'm not sure why this is happening if the timer function I am using is running on a separate thread, unless I've missed something here... 我不确定如果我使用的计时器功能在单独的线程上运行,为什么会发生这种情况,除非我在这里错过了什么...

If you create a handler (or any class you call creates a handler) it needs to be in a Thread that has a Looper on it, and has called Looper.prepare(). 如果创建处理程序(或您调用的任何类都创建处理程序),则它必须位于具有Looper且已调用Looper.prepare()的线程中。 Either TimerTask or CountDownTimer is doing that. TimerTask或CountDownTimer都可以这样做。 How to fix it depends on where you want the events to be posted to. 如何解决它取决于您要将事件发布到何处。 If you want them on the UI thread, you'll have to create the handler on the UI thread. 如果要在UI线程上使用它们,则必须在UI线程上创建处理程序。 If you want them on this thread, then you need to call Looper.prepare and Looper.loop at some point. 如果要在此线程上使用它们,则需要在某个时候调用Looper.prepare和Looper.loop。

The UI thread already has a looper (the framework starts it for you) so its always ok to make handlers there. UI线程已经有一个循环器(框架会为您启动),因此始终可以在其中进行处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio错误:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android Studio 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 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()的线程内创建处理程序” - 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 Marmalade - Can't create handler inside thread that has not called Looper.prepare() - Android Marmalade 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer Android Gingerbread:无法在未调用Looper.prepare()的线程内创建处理程序 - Android Gingerbread: 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 service Android空指针异常,并且无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android nullpointer exception and Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM