在Thread中使用处理程序

时间:2014-03-12 12:17:55

标签: java android multithreading handler

我想在我的线程中使用一个处理程序,但会产生错误。这是我的代码的一部分,函数我有问题,其中控件是一个布尔值并且是一个线程。

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();
    }

这就是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)

在何处以及如何使用循环指令?

感谢

更新

回答我是谁,我解决了问题,但现在按钮哪个控制bucle没有响应..这是听众的代码:

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;
            }
            }
        }

    };

1 个答案:

答案 0 :(得分:1)

你必须在新线程之外附加处理程序然后在线程中使用它:

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();
}

当你发布处理程序时,你的代码将运行在附加到它的处理程序的哪个线程中 (您必须在主线程中附加处理程序以避免错误)

编辑: this may help you