Search code examples
androidandroid-listviewandroid-asynctasklistview-adapter

listView having a IllegalStateException - opening a broswer just before opening a screen.


I am working on an application that would be working on the just skips a screen if the user is logged in .... and in the meanwhile in a asynctask which returns a void... check for a upgrade for the applicaiton and if there is any it would usually open a browser and download a file... and in the main thread if applicaiton has login credentials stored already then it goes to another class/screen. which usually has a listview.... The list is populated one after the another record .... but in between sometimes i get an applicaiton crash for the applicaiton how do i tackle this... and

PS. the update to the listview is being triggered from a asyncTask that usually calls a function in the main thread to update the view,...

    11-01 12:30:27.038: WARN/dalvikvm(10067): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067): FATAL EXCEPTION: main
11-01 12:30:27.038: ERROR/AndroidRuntime(10067): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class biz.graphight.graphight.CallSheetTab$CustomAdapter)]
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.ListView.layoutChildren(ListView.java:1567)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.AbsListView.onLayout(AbsListView.java:1263)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.View.layout(View.java:7035)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.View.layout(View.java:7035)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.View.layout(View.java:7035)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.View.layout(View.java:7035)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.View.layout(View.java:7035)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.os.Looper.loop(Looper.java:123)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at android.app.ActivityThread.main(ActivityThread.java:4628)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at java.lang.reflect.Method.invokeNative(Native Method)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at java.lang.reflect.Method.invoke(Method.java:521)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:879)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:637)
11-01 12:30:27.038: ERROR/AndroidRuntime(10067):     at dalvik.system.NativeStart.main(Native Method)

Any ideas... please...


Solution

  • Make sure you have fetched all the list items inside the doInBackground() and then updated the listview inside the onPostExecute() method.

    If you want to update the progress of fetching item and displaying item as and when it is downloaded then you have to update the listview inside the runOnUIThread()

    For example:

    /* Update UI */
    myActivity.this.runOnUiThread(new Runnable() {
       @Override
       public void run() {
            /* Do UI update for activity A */;
       }
     });
    

    Related: Question about runOnUiThread