Question

I want to show Toast message inside thread.. but I am getting

RunTimeException:Can't create handler inside thread that has not called Looper.prepare()

Please help me. Thanks in Advance.

Was it helpful?

Solution

Use an android.os.Handler instance to access the UI-thread from another thread:

For example:

class YourUI exends Activity {

    private Handler hm;

    @override
    public void onCreate(Bundle b) {
        // do stuff, and instantiate the handler
        hm = new Handler() {
            public void handleMessage(Message m) {
                // toast code
            }
        };
    }


    public Handler returnHandler(){
        return hm;
    }
}

In a non-UI thread, use this:

YourUI.getHandler().sendEmptyMeassage(0);

OTHER TIPS

Try below code in your thread

runOnUiThread(new Runnable() 
        {                
            @Override
            public void run() 
            {
                //Your toast code here
            }
        });

What happens that Thread is a non GUI thread and you can't access GUI element from non-GUI Threads

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top