Android - Can't Create Handler Inside Thread That Has Not Called Looper.prepare()
I am developing a simple application using Google Maps V2 API just to get basics and I am facing this error: 09-09 21:21:41.154: E/AndroidRuntime(3796): FATAL EXCEPTION: Thread-44
Solution 1:
I had a similar issue, problem was that I was having series of Thread from Main Thread. Actually Main UI Thread started a service, which ran on a service and subsequently started another thread, which finally had a FileObserver (another thread), I had to communicate to UI thread by popping a toast message. I tried for several hours, and tried below, it worked as a charm.
//Let this be the code in your n'th level thread from main UI thread
Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
publicvoidrun() {
Toast.makeText(context, "Your message to main thread", Toast.LENGTH_SHORT).show();
}
});
The main key here is the getMainLooper() function of Looper class, which will provide you the Looper against the Main UI thread.
Solution 2:
You cannot execute an AsyncTask
from a background thread. See the "Threading Rules" section of the AsyncTask
documentation.
Solution 3:
Do this in your do in background...
runOnUiThread(new Runnable() {
publicvoidrun()
{
if (items.equals("null"))
{
Toast.makeText(getApplicationContext(), "No Event Found",Toast.LENGTH_LONG).show();
}
else {
}
}
});
Post a Comment for "Android - Can't Create Handler Inside Thread That Has Not Called Looper.prepare()"