简体   繁体   English

在线程内使用处理程序

[英]Use handler inside Thread

I want to use a handler inside my thread but produces error. 我想在线程中使用处理程序,但是会产生错误。 this is part of my code, function where i have the problem, where control is a boolean and bucle a Thread. 这是我的代码的一部分,我遇到的问题是函数,控制是布尔值,而bucle Thread。

private void mainbucle() {

        bucle = new Thread(new Runnable() {
            @Override
            public void run() {

                while (control == true) {


                    Handler handler2 = new Handler();
                    handler2.postDelayed(new Runnable() {
                        public void run() {

                                              //MY ACTIONS//

                        }
                    }, 1000);

                }
            }
        }, "bucle telo");
        bucle.start();
    }

And this the logcat: 这是日志:

03-12 13:22:44.321: E/AndroidRuntime(18987): FATAL EXCEPTION: bucle telo
03-12 13:22:44.321: E/AndroidRuntime(18987): Process: com.example.uwbprototipe, PID: 18987
03-12 13:22:44.321: E/AndroidRuntime(18987): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-12 13:22:44.321: E/AndroidRuntime(18987):    at android.os.Handler.<init>(Handler.java:200)
03-12 13:22:44.321: E/AndroidRuntime(18987):    at android.os.Handler.<init>(Handler.java:114)
03-12 13:22:44.321: E/AndroidRuntime(18987):    at com.example.uwbprototipe.MainActivity$4.run(MainActivity.java:224)
03-12 13:22:44.321: E/AndroidRuntime(18987):    at java.lang.Thread.run(Thread.java:841)
03-12 13:22:44.911: E/ActivityThread(18987): Activity com.example.uwbprototipe.MainActivity has leaked IntentReceiver com.example.uwbprototipe.MainActivity$1@43034be0 that was originally registered here. Are you missing a call to unregisterReceiver()?
03-12 13:22:44.911: E/ActivityThread(18987): android.app.IntentReceiverLeaked: Activity com.example.uwbprototipe.MainActivity has leaked IntentReceiver com.example.uwbprototipe.MainActivity$1@43034be0 that was originally registered here. Are you missing a call to unregisterReceiver()?
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:805)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:606)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1430)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1410)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1404)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:467)
03-12 13:22:44.911: E/ActivityThread(18987):    at com.example.uwbprototipe.MainActivity.onCreate(MainActivity.java:71)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.Activity.performCreate(Activity.java:5231)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.os.Looper.loop(Looper.java:136)
03-12 13:22:44.911: E/ActivityThread(18987):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-12 13:22:44.911: E/ActivityThread(18987):    at java.lang.reflect.Method.invokeNative(Native Method)
03-12 13:22:44.911: E/ActivityThread(18987):    at java.lang.reflect.Method.invoke(Method.java:515)
03-12 13:22:44.911: E/ActivityThread(18987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-12 13:22:44.911: E/ActivityThread(18987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-12 13:22:44.911: E/ActivityThread(18987):    at dalvik.system.NativeStart.main(Native Method)

Where and How use loop instructions? 在何处以及如何使用循环指令?

thanks 谢谢

UPDATE UPDATE

With answer of Who am I, i solved the problem but now button which control bucle not responds..this is code of its listener: 随着我是谁的回答,我解决了问题,但是现在控制气泡的按钮没有响应..这是它的侦听器的代码:

private View.OnClickListener btnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btnStart: {
                control = true;
                enableButtons(true);
                mainbucle();
                break;
            }
            case R.id.btnStop: {
                control = false;
                enableButtons(false);
                bucle = null;
                break;
            }
            }
        }

    };

you must attach handler outside of new thread then use it inside of thread: 您必须在新线程外附加处理程序,然后在线程内使用它:

private void newThread() {

    Handler h = new Handler();
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
             //...
             h.postDelayed(new Runnable() {
                    public void run() {
                       //...
                    }
                }, 1000);

        }
    });
    t.start();
}

when you post handler, your code will be run in which thread that handler attached to it 当您发布处理程序时,您的代码将在该处理程序附加到的线程中运行
(you must attach handler inside main thread to avoid errors) (您必须在主线程内附加处理程序以避免错误)

EDIT : this may help you 编辑: 这可能对您有帮助

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

相关问题 在Android中创建线程内的处理程序 - Create a Handler Inside Thread in Android 如何在android中使用处理程序在主线程中工作? - how to use handler in android to work in main thread? 无法在未调用Looper prepare的线程内创建处理程序 - Can't create handler inside thread that has not called Looper prepare 无法在未调用Looper prepare的线程内创建处理程序 - Can't create handler inside thread that has not called Looper prepare 如何在junit中使用未捕获的异常处理程序进行多线程测试? - How to use an uncaught exception handler for a multi-thread test in junit? Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread 无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog 如何在服务器/客户端(JFrame)中使用线程? - How to use Thread inside server/client (JFrame)? 如何在线程和小程序内部使用双缓冲 - How to use double buffering inside a thread and applet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM