涉及适配器的奇怪的 IllegalStateException

Weird IllegalStateException involving adapter

我有一个 ListView,我在其中提供了一些选项,具体取决于从数据库中获取的数据。单击该 ListView 的项目时,它应该打开一个 Activity。列表视图通常填充有两个项目。当我单击其中一项时,一切正常。但是,如果我离开 Activity,然后单击其他项目,我会得到这个异常:

08-10 13:00:35.724  22877-22877/? E/MessageQueue-JNI﹕ 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. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131558531, class android.widget.ListView) with Adapter(class com.infaplic.lpi.Adapters.ListaTributosAdapter)]
        at android.widget.ListView.layoutChildren(ListView.java:1555)
        at android.widget.AbsListView.onTouchUp(AbsListView.java:3617)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
        at android.view.View.dispatchTouchEvent(View.java:7837)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2072)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1519)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2467)
        at com.infaplic.lpi.activities.SwipeActivity.dispatchTouchEvent(SwipeActivity.java:70)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2020)
        at android.view.View.dispatchPointerEvent(View.java:8017)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3984)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3863)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3549)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3606)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3473)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3450)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3423)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5635)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5615)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5586)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5715)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:123)

它指向这一行:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    onSwipeTouchListener.getGestureDetector().onTouchEvent(ev);
    return super.dispatchTouchEvent(ev);//Get the exception here.
}

ListView 是这样填充的:

adapter=new ListaTributosAdapter(c, SwipeActivity.beanElementosTributarios);
    listView.setAdapter(adapter);

其中SwipeActivity.beanElementosTributarios是一个静态数组列表,里面存放着我想在列表视图中呈现的东西。此时数组修改的地方只有一处:

if(SwipeActivity.beanElementosTributarios==null){
                SwipeActivity.beanElementosTributarios=new ArrayList<>();
            }
            SwipeActivity.beanElementosTributarios.add(bean);

您可以在应用程序的另一点更改arrayList,但应用程序的那个点还没有达到。当我调试时,我看到 arrayList 有两个项目,这是应该的。

这个异常让我快要疯了。

有人可以帮助我吗? (如需查看其他部分代码,请追问)

谢谢...

编辑:如果我单击列表中的第一项,则显示正常。当我单击第二个元素时,出现异常。但是,如果我先单击列表视图的第二个元素,它会正常显示,但如果我之后单击第一个元素,则会引发异常。

编辑 2: 它不是那个问题的重复。除了 UI 线程外没有其他线程,此时适配器的 arrayList 没有更改。

编辑 3:

找到这个:https://code.google.com/p/android/issues/detail?id=71936。也许是一个错误?

更改列表中的数据后,您必须在适配器上调用 notifyDataSetChanged()。错误提示您的适配器中的数据已更新,但视图显示旧数据。