Reputation: 1966
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.
Upvotes: 0
Views: 9845
Reputation: 7635
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);
Upvotes: 4
Reputation: 15269
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
Upvotes: 11