Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

None of my AsyncTasks get callbacks to onPostExecute

Tags:

android

When I have a fresh install of my application, NONE of my AyncTasks seem to work. It doesn't matter which it is, they all fail with the message "sending message to a dead thread. " They're in all sorts of activities doing all sorts of different work, but they all fail.

If I look at the threads in the thread viewer of DDMS, all 5 or so AsynchTask threads appear to be idle and waiting for work.

If I force close the application, and start it back up, they all start working.

They're all started on the UI thread.

Any ideas?

like image 843
Plantage Avatar asked Jan 28 '26 13:01

Plantage


1 Answers

This is due to a bug in AsyncTask in the Android framework. AsyncTask.java has the following code:

private static final InternalHandler sHandler = new InternalHandler();

It expects this to be initialized on the main thread, but that is not guaranteed since it will be initialized on whichever thread happens to cause the class to run its static initializers. In your case, you're probably causing it to be initialized on the worker thread you created.

A simple workaround is to add the following code to the application's onCreate method:

Class.forName("android.os.AsyncTask");

This will force AsyncTask to be initialized in the main thread. I filed a bug on this in the android bug database. See http://code.google.com/p/android/issues/detail?id=20915.

like image 109
Jonathan Perlow Avatar answered Jan 30 '26 04:01

Jonathan Perlow