androidandroid-monkey

Android monkey causes adapter notification exception in android.widget.HeaderViewListAdapter


I am having the common problem:

java.lang.IllegalStateException: The content of the adapter has changed but List
View did not receive a notification. Make sure the content of your adapter is no
t modified from a background thread, but only from the UI thread. [in ListView(2
131427573, class android.widget.ListView) with Adapter(class android.widget.Head
erViewListAdapter)]

But the adapter is not my code, but in android.widget.HeaderViewListAdapter This is using Jellybean.

I read through the source code of HeaderViewListAdapter, ListAdapter and ListView. The IllegalStateException is thrown when the item count inListView is not equal to the count provided by the ListAdapter. In this case, the ListAdapter is the HeaderViewListAdapter. The HeaderViewListAdapters count is the count of the original ListAdapter passed by the client code, plus the size of the header and footer.

I traced through my code. All access to the ListView is on the UI thread, and is always followed by notifyDataSetChanged() to the adapter. I am using one footer.

This doesn't occur in normal usage. Is it due to Monkey? But how can Monkey modify my variables from other threads?

I removed the footer by removing the call to addFooterView(). Monkey no longer triggers the exception. Should I remove the call to addFooterView() at some point?


Solution

  • You can try adding something like this to your ListView:

        @Override
    protected void layoutChildren() {
        try {
            super.layoutChildren();
        } catch (IllegalStateException e) {
            ZLog.e(LOG, "This is not realy dangerous problem");
        }
    }
    

    If you add a headerview or a footerview ListView, and when you notifyDataSetChanged(), it will change the mItemCount to the item count of real adapter, but the right side will return the fake itemcount which has added the headercount and footercount.

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/ListView.java?av=f#1538