Android: IllegalStateException - When is it thrown?

It does tell you the class the error occurs in: ch.uzh.csg.games4blue.gamebase.view.UserView contains a class called UserAdapter inside of it.

Let me try to explain what is happening here - When you create a ListView with an Adapter backing it up, the ListView gets its data by asking the adapter for it. Every time you scroll, the ListView is asking the Adapter for the new items that you scrolled into.

Normally you create the ListView, you create the Adapter, you set the adapter on the listview, and you are done. Android OS takes care of the rest. What you are attempting to do, however, is a bit more complicated. You are trying to occasionally update the data in the adapter. You can imagine that the listview would need to be informed if the adapter data changed, right? If you are displaying a list with 2 items, and you suddenly add 2 more items to the adapter, then the list should now be displaying 4 items! However, it would be really inefficient if the listview had to be continually checking to see if the adapter has changed, right? So what needs to happen is the listview will assume that the adapter doesn't change, and the adapter will notify the listview if it does change. (This agreement between the listview and the adapter allows other benefits, such as the listview can cache some of the items retrieved from the adapter for quick access. Aka if there are 10 list items displayed on the screen, the list may have actually already requested 14 items. That way if you scroll up or down, for at least 2 items in either direction the ListView already has the data to display!)

What you have failed to do, however, is to notify the listview that the data inside of the adapter changed. Somewhere inside UserAdapter or UserView the data is being changed! To make this easy on everyone, perhaps you could post the full UserView class? My bet is that you have some data object such as an ArrayList, as a private variable inside of the UserView class. Inside of the UserAdapter class you are providing this data to the ListView. However, somewhere inside of the UserView class you are modifying this dataset, after you have already provided it to the ListView.

Disclaimer: I'm mostly copying other answers here, but I think I can provide a more clear view of what is happening if I combine the 2-3 answers given

Disclaimer 2: I'm tired and this seems poorly written. Making it a wiki in the hopes someone cleans up my late night mess. If this is totally unintelligible just say so and ill delete it


You can see the error in your stacktrace :

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.

You should look into one of your thread and make it synchronized with your UI thread.

The way to do this in Android is with Handler.

http://developer.android.com/guide/appendix/faq/commontasks.html#threading


Indeed, this kind of error is sent when you modify the content of your adapter, without notifying your listview or any other view using an adapter that its content should be updated.
Try to call .notifyDataSetChanged() on the adapter.


Where you are using ch.uzh.csg.games4blue.gamebase.view.UserView$UserAdapter.