Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would an AsyncTask's postExecute method not run in the UI thread?
    text
    copied!<p>I have a nested inner class that extends AsyncTask to run a db query in the background. In the post execute method I am calling a parent's method to update the view something like this</p> <pre><code>private class QueryRunner extends AsyncTask&lt;Void,Void,Cursor&gt; { @Override protected Cursor doInBackground(Void... voids) { return getContentResolver().query(LeadContentProvider.buildUri(app.getEntityId()),new String[]{LeadContentProvider._ID},null,null,LeadContentProvider.LEAD_STATUS_DATETIME +" desc"); } @Override protected void onPostExecute(Cursor c) { onCursorLoaded(c); } } </code></pre> <p>The onCursorLoaded method looks like:</p> <pre><code>private void onCursorLoaded(Cursor c) { mPagerAdapter = new LeadDetailsFragmentPagerAdaper(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); mPager.setCurrentItem(iIndex, false); } </code></pre> <p>Most of the time this works fine - but some users have crashes with this stack trace:</p> <pre><code>java.lang.IllegalStateException: Must be called from main thread of process at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1392) at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:139) at android.support.v4.view.ViewPager.populate(ViewPager.java:804) at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:344) at com.servicemagic.pros.activities.LeadDetails.onCursorLoaded(LeadDetails.java:205) at com.servicemagic.pros.activities.LeadDetails.access$200(LeadDetails.java:25) at com.servicemagic.pros.activities.LeadDetails$QueryRunner.onPostExecute(LeadDetails.java:196) at com.servicemagic.pros.activities.LeadDetails$QueryRunner.onPostExecute(LeadDetails.java:170) 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:150) at android.os.HandlerThread.run(HandlerThread.java:60) </code></pre> <p>So - why is the postExecute() method not called on the MainThread?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload