无法找到从后台进程修改适配器内容的位置

时间:2014-07-03 10:08:56

标签: android listview

由于我的列表适配器是从后台线程修改的,我每天至少得到1个我的应用程序崩溃的报告,但是我找不到课堂上发生这种情况的地方。

我该怎么做才能找到这个问题的所在?堆栈跟踪没有帮助。

  

java.lang.IllegalStateException:适配器的内容有   已更改,但ListView未收到通知。确保   您的适配器的内容不会从后台线程修改,但是   仅来自UI线程。 [在ListView(2131165368,类   android.widget.ListView)with Adapter(class   com.xxx.xx.xx.xxListAdapter)]   android.widget.ListView.layoutChildren(ListView.java:1538)at   android.widget.AbsListView.onTouchEvent(AbsListView.java:3524)at   android.view.View.dispatchTouchEvent(View.java:7127)at   android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)at   android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)at   android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)at   android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)at   android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)at   com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1930)   在   com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1384)   在android.app.Activity.dispatchTouchEvent(Activity.java:2402)at   com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1878)   在android.view.View.dispatchPointerEvent(View.java:7307)at   android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3184)   在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3129)   在   android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4165)   在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4144)   在   android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4236)   在   android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)   在android.os.MessageQueue.nativePollOnce(Native Method)at   android.os.MessageQueue.next(MessageQueue.java:125)at   android.os.Looper.loop(Looper.java:124)at   android.app.ActivityThread.main(ActivityThread.java:4781)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:3152)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:2919)at   dalvik.system.NativeStart.main(原生方法)

1 个答案:

答案 0 :(得分:0)

可能,您的列表适配器正在从后台线程中进行修改,原因是您在后台线程中调用notifyDataSetChanged()而不是在主UI线程中,左右解决方案是将其移动到UI线程或只需用runOnUiThread()包装它:

......
     runOnUiThread(new Runnable() 
         { 
            public void run() 
                {
                   notifyDataSetChanged();
                }
         });
... ...

希望这会对你有所帮助。