Question

I have been receiving a large number of bug reports from users reported from force close dialogs that state something like the following:

java.lang.ExceptionInInitializerError
at com.MyRequest.execute(MyRequest.java:59)
at org.nsdev.MySyncAdapter.onPerformSync(MySyncAdapter.java:90)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:164)
  Caused by: java.lang.RuntimeException: 
  Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)

Now, I've been trying to figure out how I should best resolve this issue. Inside my onPerformSync implementation I am creating a number of AsyncTasks and executing them. I suspect my problem is that I'm not waiting for these tasks to complete before returning from onPerformSync. I tried to do the following:

Looper.prepare();

// Execute async tasks

Looper.loop();

And then I set up a counter and when that counter decrements to zero, call Looper.myLooper().quit() inside the callback from the async tasks.

But doing this causes my application to crash even harder with the following runtime error:

java.lang.RuntimeException: Main thread not allowed to quit
    at android.os.MessageQueue.enqueueMessage(MessageQueue.java:175)
    at android.os.Looper.quit(Looper.java:173)
    at org.nsdev.MySyncAdapter$1.requestFinished(MySyncAdapter.java:89)
    at com.MyAsyncTask.onPostExecute(MyAsyncTask.java:84)
    at android.os.AsyncTask.finish(AsyncTask.java:417)
    at android.os.AsyncTask.access$300(AsyncTask.java:127)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

Any idea how to correctly accomplish my sync?

Was it helpful?

Solution

I know this post is old, but for others who come across this thread, what about just not calling looper.quit() on the main thread? The following works for me from a SyncAdpater Service spawning several AsyncTasks, although there may be other reasons not to do this in terms of performance...

Handler h = new Handler(Looper.getMainLooper());
            h.post(new Runnable() {
                public void run() {

                    // AsyncTask... execute();
                }
            });

OTHER TIPS

Actually you can't. AsyncTask uses main UI thread and relies on Handler and Looper which is not available there. Use pure Thread implementation, AsyncTask should be used only in the activities.

The response of abloc solve the problem, but the problem with this solution is that the UI freezes, this problem is present in Android 2.2 and Android 2.3. The trick I found was to invoke a constructor of AsyncTask in my class SyncAdapter and used Looper.prepare():

/**
 * Constructor. Obtains handle to content resolver for later use.
 */
public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);
    mHandler = new Handler();
    //Crear un dummy AsyncTask
    new AsyncTask<Void, Void,String>() {
        @Override
        protected String doInBackground(Void... params) {
            return null;
        }
    }.execute(null,null);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top