2

I am getting this error when I call listview.setAdapter(adapter); from an AsyncTask's onPostExecute() method:

07-12 19:29:38.147: E/AndroidRuntime(8478): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

*Note: This crash only happens in API level 10, but not in the latest APIs (on API 17 this runs fine).

This is the onPostExecute():

@Override
        protected void onPostExecute(String[] result) {
            super.onPostExecute(result);
            // TODO: check this.exception 
            er=result;//er is a global variable
            gff=true;//gff is a global variable
            asynchelp(er);//function in the UI thread
            myPd_ring.dismiss();//dismissing a spinning ring
        }

asynchelp() is below:

public void asynchelp(String[] er){
        String iamoutofnames[]=null;
        for(int q = 0; q <er.length; q++){
            HashMap<String, String> map = new HashMap<String, String>();
        iamoutofnames=null;
        int len;
        if(er[q].contains("sandy")){
            //calculations
            map.put("col_1", iamoutofnames[len]);
        }else{
            //calculations
        map.put("col_1", iamoutofnames[1]);
            }
        fillMaps.add(map);
        lv.setOnItemClickListener(onListClick);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
    lv.setAdapter(adapter);//CRASHES ON THIS LINE**************

}

What is wrong with the code?

EDIT: The stacktrace:

    07-12 19:29:37.996: W/System.err(8478): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-12 19:29:37.996: W/System.err(8478):     at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
07-12 19:29:38.006: W/System.err(8478):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
07-12 19:29:38.006: W/System.err(8478):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
07-12 19:29:38.006: W/System.err(8478):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
07-12 19:29:38.006: W/System.err(8478):     at android.view.View.invalidate(View.java:5279)
07-12 19:29:38.006: W/System.err(8478):     at android.widget.AbsListView.resetList(AbsListView.java:1150)
07-12 19:29:38.006: W/System.err(8478):     at android.widget.ListView.resetList(ListView.java:511)
07-12 19:29:38.006: W/System.err(8478):     at android.widget.ListView.setAdapter(ListView.java:440)
07-12 19:29:38.006: W/System.err(8478):     at com.example.kiva.ListViewA.asynchelp(ListViewA.java:519)
07-12 19:29:38.006: W/System.err(8478):     at com.example.kiva.ListViewA$RetreiveFeedTask.onPostExecute(ListViewA.java:1330)
07-12 19:29:38.006: W/System.err(8478):     at com.example.kiva.ListViewA$RetreiveFeedTask.onPostExecute(ListViewA.java:1)
07-12 19:29:38.006: W/System.err(8478):     at android.os.AsyncTask.finish(AsyncTask.java:417)
07-12 19:29:38.006: W/System.err(8478):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-12 19:29:38.006: W/System.err(8478):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-12 19:29:38.016: W/System.err(8478):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 19:29:38.016: W/System.err(8478):     at android.os.Looper.loop(Looper.java:130)
07-12 19:29:38.016: W/System.err(8478):     at android.os.HandlerThread.run(HandlerThread.java:60)

07-12 19:29:40.096: W/FlurryAgent(8478): onEndSession called without context from corresponding onStartSession
07-12 19:29:40.206: E/WindowManager(8478): Activity com.example.kiva.ListViewA has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40631600 that was originally added here
07-12 19:29:40.206: E/WindowManager(8478): android.view.WindowLeaked: Activity com.example.kiva.ListViewA has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40631600 that was originally added here
07-12 19:29:40.206: E/WindowManager(8478):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
07-12 19:29:40.206: E/WindowManager(8478):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
07-12 19:29:40.206: E/WindowManager(8478):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-12 19:29:40.206: E/WindowManager(8478):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.Dialog.show(Dialog.java:241)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ProgressDialog.show(ProgressDialog.java:107)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
07-12 19:29:40.206: E/WindowManager(8478):  at com.example.kiva.ListViewA.topfeeds(ListViewA.java:1598)
07-12 19:29:40.206: E/WindowManager(8478):  at com.example.kiva.ListViewA.onCreate(ListViewA.java:278)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-12 19:29:40.206: E/WindowManager(8478):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-12 19:29:40.206: E/WindowManager(8478):  at android.os.Looper.loop(Looper.java:130)
07-12 19:29:40.206: E/WindowManager(8478):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-12 19:29:40.206: E/WindowManager(8478):  at java.lang.reflect.Method.invokeNative(Native Method)
07-12 19:29:40.206: E/WindowManager(8478):  at java.lang.reflect.Method.invoke(Method.java:507)
07-12 19:29:40.206: E/WindowManager(8478):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-12 19:29:40.206: E/WindowManager(8478):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-12 19:29:40.206: E/WindowManager(8478):  at dalvik.system.NativeStart.main(Native Method)
vergil corleone
  • 1,091
  • 3
  • 16
  • 34
  • My best guess is that you created the `ListView` on a background thread and did not add it to the view hierarchy at that point. – CommonsWare Jul 12 '13 at 14:28
  • @CommonsWare The `ListView` was created in the `onCreate()` function of the UI thread itself. Also this code works perfectly on API level 17, that must mean there is some limitation in API level 10 which my code is violating..? – vergil corleone Jul 12 '13 at 14:34
  • @CommonsWare Also when I circumvented the asynctask and passed some dummy values to the adapter, it worked perfectly. – vergil corleone Jul 12 '13 at 14:42
  • what if you call lv.setAdapter(adapter); from the UI (runOnUiThread) thread itself? – Rajeev Jul 12 '13 at 16:08
  • Also getting this problem (on older Android devices) — invalid thread access despite being inside AsyncTask.onPostExecute(). Seems to be a bug in Android. Stack (in case someone is interested): https://gist.github.com/andreyvit/b762c8bf60b4b26f5c4d – Andrey Tarantsov Oct 22 '13 at 03:35
  • possible duplicate of [Android - a Thread is crushing the application](http://stackoverflow.com/questions/14405127/android-a-thread-is-crushing-the-application) – Mr_and_Mrs_D Nov 17 '13 at 20:16

1 Answers1

1

This is what I used in asynchelp(); to solve the error:

runOnUiThread(new Runnable() {
         public void run() {
    lv.setAdapter(adapter);//lv needs to be declared as final
       }
    });
vergil corleone
  • 1,091
  • 3
  • 16
  • 34