I found the same problem on Android 2.3.x devices and here is the crash log:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. at android.view.ViewRoot.checkThread(ViewRoot.java:2934) ... at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:130) at android.os.HandlerThread.run(HandlerThread.java:60)
The log shows that onProgressUpdate
and onPostExecute
run on HandlerThread
, which is essentially a workflow with a custom Looper
. Therefore, why the failure occurs.
Therefore, in your case, it is likely that the AsyncTask
internal handler AsyncTask
bound to a non-main looper associated with the workflow, for example HandlerThread
and onUpdateProgress
instead processed in the onUpdateProgress
.
I found that this error appears everywhere on Android 2.3 devices. So I checked the AsyncTask
source code in 2.3 and found this:
private static final InternalHandler sHandler = new InternalHandler(); private static class InternalHandler extends Handler { @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"}) @Override public void handleMessage(Message msg) { ... } }
The likelihood that the internal handler may be associated with a non-main looper.
I also checked the latest AsyncTask
source code and saw a change:
private static class InternalHandler extends Handler { public InternalHandler() { super(Looper.getMainLooper()); } ... }
The InternalHandler
constructor eliminates the possibility that it may be associated with a non-main looper, so onUpdateProgress
behaves normally on Android 2.3 mail devices.
source share