user3243651
user3243651

Reputation: 241

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.

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

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

    };

Upvotes: 0

Views: 1003

Answers (1)

Smile
Smile

Reputation: 375

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

Upvotes: 1

Related Questions