涉及适配器的奇怪的IllegalStateException

时间:2015-08-10 11:21:06

标签: android android-listview android-adapter

我有一个ListView,我提供了一些选项,具体取决于从数据库获取的数据。单击该ListView的项目时,它应该打开一个活动。列表视图通常包含两个项目。当我点击其中一个项目时,一切正常。但是,如果我退出活动,然后点击其他项目,我会得到以下异常:

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有两个项目,应该这样。

我对这个例外感到疯狂。

任何人都可以帮助我吗? (如果您需要查看代码的其他部分,请询问)

谢谢...

编辑:如果我点击列表中的第一项,则显示确定。当我点击SECOND元素时,异常会引发异常。但是,如果我首先单击列表视图的SECOND元素,它会正常显示,但如果我在此之后单击FIRST元素,则会引发异常。

编辑2: 这不是该问题的重复。 UI线程没有其他线程,此时适配器的arrayList不会更改。

编辑3:

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

1 个答案:

答案 0 :(得分:1)

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