5

I've read alot about this one on SO but i havent been able to find a solution in my case. Here's the setup:

Activity A, contains the List and the data being displayed. When my data manager's async task finishes updating, the following happens:

protected void onPostExecute(Void v) {
        super.onPostExecute(v);

        if (editedDataSectionRows == null)
            editedDataSectionRows = new int[] {0, 0, 0, 0, 0, 0, 0};

        mData= editedData;
        mSectionRows = editedDataSectionRows;

        if (mCallback != null) {
            mCallback.dataUpdated();
        }
    }

When the data is updated, activity A gets a callback and does the following:

@Override
public void dataUpdated() {
    mData = mDataManagerInstance.getData();
    mDataSectionCount = mDataManagerInstance.getDataSectionRows();
    mListAdapter.notifyDataSetChanged();
}

mData is an ArrayList containing all the "rows" and mDataSectionCount is an int[] containing information about how mData should be split up into sections.

Now, Activity A has an optionsmenu that can open up a second Activity. Activity B.

Every now and then when running Monkey and Activity B finishes. My onResume for Activity A gets called:

@Override
protected void onResume() {
    super.onResume();
    mDataManagerInstance = DataManager.getInstance();

    mDataManagerInstance.setDataUpdatedListener(this);
    mDataManagerInstance.updateData();

    this.invalidateOptionsMenu();
}

(updateData just fires away my AsyncTask if its not already running) And then shit hits the fan... :

FATAL EXCEPTION: main
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(-1, class com.applidium.headerlistview.HeaderListView$InternalListView) with Adapter(class se.*.android.activities.ListActivity$1)]
        at android.widget.ListView.layoutChildren(ListView.java:1582)
        at android.widget.ListView.commonKey(ListView.java:2104)
        at android.widget.ListView.onKeyDown(ListView.java:2085)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2620)
        at android.view.View.dispatchKeyEvent(View.java:7149)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1458)
        at android.widget.ListView.dispatchKeyEvent(ListView.java:2070)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1462)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1898)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1375)
        at android.app.Activity.dispatchKeyEvent(Activity.java:2356)
        at com.actionbarsherlock.app.SherlockFragmentActivity.dispatchKeyEvent(SherlockFragmentActivity.java:122)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1825)
        at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3585)
        at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3555)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2805)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:4787)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
        at dalvik.system.NativeStart.main(Native Method)

Any ideas?

  • Possible duplicate of [Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"](http://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed) – rds May 11 '16 at 22:11

1 Answers1

1

try do that in ui thread

runOnUiThread(new Runnable(){
        public void run() {
            mListAdapter.notifyDataSetChanged();
        }
    });
thealeksandr
  • 1,686
  • 12
  • 17
  • But onPostExecute is supposed to run on the UI-thread already right? (Ill however add this and check too see if it solves my issues.) – Magnus Söderlund May 15 '14 at 10:15
  • Just checked, no change. I still receive the same exception when Activity B closes and onResume is called – Magnus Söderlund May 15 '14 at 10:29
  • Yeah it's strange. Did you check that new data is valid? – thealeksandr May 15 '14 at 12:10
  • Yeah it seems to be fine. The strange thing is that it only happens when returning from Activity B that A started, never when entering Activity A in the first place... – Magnus Söderlund May 15 '14 at 12:31
  • I red something. Are you sure that you don't change adapter in not ui thread in somewhere else and always have to call notifyDataSetChanged() when data changed – thealeksandr May 15 '14 at 12:58
  • Yes, ive checked my code several times over. There is one single place where the data is modified and from there the activity gets a callback so it can collect the data itself and tell the adapter notifyDataSetChanged. So the adapters data never ever changes until this happens. – Magnus Söderlund May 16 '14 at 06:21