Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: got CalledFromWrongThreadException in onPostExecute() - How could it be?

Tags:

I have an app in production for a few weeks, using ACRA, and I had zero errors until one strange error reported today.

I've got:

    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

coming from this method in the stack trace (retraced):

at my.app.CountdownFragment$1.void onPostExecute(java.lang.Object)(SourceFile:1) 

And this is the relevant source snippet:

    private void addInstructionsIfNeeded() {     if (S.sDisplayAssist) {         new AsyncTask<String, Void, String>() {              @Override             protected String doInBackground(String... params) {                 return null;             }              /*              * runs on the ui thread              */             protected void onPostExecute(String result) {                  Activity a = getActivity();                  if (S.sHelpEnabled && a != null) {                      in = new InstructionsView(a.getApplicationContext());                      RelativeLayout mv = (RelativeLayout) a                             .findViewById(R.id.main_place);                      mv.addView(in.prepareView());                 }              };          }.execute("");     } } 

Where addInstructionsIfNeeded() is called from a handler dispatched message (the UI thead).

  • onPostExecute() runs on the UI thread, so why I've got "wrong thread"?
  • This code ran already on more than 150 devices, and more than 100000 times (according to Flurry), and never had this error.
  • The originating device is Samsung SGH-I997 running SDK 4.0.4

My question is: How could it be?

EDIT: This all happens in a fragment

like image 739
Amir Uval Avatar asked May 03 '12 06:05

Amir Uval


2 Answers

i was suffering from the same problem, this is another android framework bug...

what is happening:

in certain circumstances an application can have more than one "looper" and therefore more than one "UI thread"

--side note-- i am using the term "UI thread" in the loosest of senses in this answer, since when people say "UI thread" they usually mean main or entry thread, Android like many of other OS before it, allow for for multiple message pumps (called a Looper in Android, see: http://en.wikipedia.org/wiki/Event_loop) for different UI trees, as such android for all intents and purposes is capable of running more than one "UI thread" in certain circumstances and using that term leads to rampant ambiguities... --end side note--

this means:

since an application can have more than one "UI thread" and an AsyncTask always "Runs on the UI thread" [ref], someone decided [poorly] that instead of the AsyncTask always running on its creation thread (which in 99.999999% of cases would be the correct "UI thread") they decided to use hocus pocus (or a poorly crafted shortcut, you decide) to execute on the "main looper"..

example:

    Log.i("AsyncTask / Handler created ON: " + Thread.currentThread().getId());     Log.i("Main Looper: " + Looper.getMainLooper().getThread().getId() + "      myLooper: "+ Looper.myLooper().getThread().getId());      new AsyncTask<Void, Void, Void>() {          @Override         protected Void doInBackground(Void... params) {             Log.i("doInBackground ran ON: " + Thread.currentThread().getId());             // I'm in the background, all is normal              handler.post(new Runnable() {                  @Override                 public void run() {                     Log.i("Handler posted runnable ON: " + Thread.currentThread().getId());                     // this is the correct thread, that onPostExecute should be on                 }             });              return null;         }          @Override         protected void onPostExecute(Void result) {             Log.i("onPostExecute ran ON: " + Thread.currentThread().getId());             // this CAN be the wrong thread in certain situations         }      }.execute(); 

if called from the bad situation described above the output will look something like this:

    AsyncTask / Handler created ON: 16     Main Looper: 1      myLooper: 16     doInBackground ran ON: 12     onPostExecute ran ON: 1     Handler posted runnable ON: 16 

that's a huge FAIL for AsyncTask

as shown this can be mitigated using a Handler.post(Runnable) in my specific case the duality of my "UI thread" situation was caused by the fact that I was creating a dialog in response to a JavaScript interface method called from a WebView, basically: the WebView had its own "UI thread" and that was the one that i was currently running on..

from what i can tell (without really caring about or reading into it too much) it seems that the AsyncTask class' callback methods in general run off a single statically instantiated handler (see: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/os/AsyncTask.java#AsyncTask.0sHandler), which means that it is always going to execute on the "main thread" or "entry thread" which they incorrectly refer to as the "UI thread" (which is presumed as any thread where UI interactions take place, eg. multiple threads in this case) this is both shoddy craftsmanship and shoddy documentation from the android team... weak sauce, the sauce is weak

hope this helps you -ck

like image 60
ckozl Avatar answered Dec 13 '22 02:12

ckozl


Had the same issue. Solved in my case

Briefly explanation:

  1. Running AsynckTask for the very first time on non UI thread with looper leads to loading AsyncTask.class and initialization sHandler to handler constructed on that non UI looper.
  2. Now sHandler is connected to that non UI thread for ANY instance of AsyncTask subclasses and onPreExecute, onProgressUpdate and onPostExecute methods will be invoked on that non UI thread (unless AsyncTask.class will be unloaded)
  3. Any attempt to deal with UI inside any of the above methods will lead to crash with android.view.ViewRootImpl$CalledFromWrongThreadException
  4. To avoid such situation one should always run (at least for the very first time) AsyncTask on UI thread in order to let AsyncTask's sHandler-field be initialized with UI's looper

The story:

There were two production apps: A - main android app and B - some utilty app.

After integration app B ito app A we received a lot of crashes:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

for method running from AsynckTask.onPostExecute()

After some investigation it appeared that utility app B used AsyncTask within its HandlerThread

The traces was found in AsyncTask's source code:

private static final InternalHandler sHandler = new InternalHandler(); 

This is the handler which is used to send onPostExecute() to UI thread.

This handler is static and it will be initialized during class loading i.e. first new AsyncTask() appearance

It means that onPostExecute will always be posted to that thread where new AsyncTask() was called for the first time (unless AsyncTask.class will be unloaded and loaded again)

In my case the flow was something like this:

1 - starting app A 2 - initializing B form A 3 - B creates its own HandlerThread and launches AsyncTask <- now onPostExecute wil be posted to this HandlerThread no matter where from an instance of AsyncTask will be launched in future 4 - create AsyncTask in the app A for a long operation and update UI in its onPostExecute 5 - when executing onPostExecute() the CalledFromWrongThreadException is thrown 

Then a friend of mine showed me related documentation from android.developers (Threading rules section):

The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread.

Hope it can help to make clear the situation)

like image 35
vir us Avatar answered Dec 13 '22 01:12

vir us