我经常在android listview上随机崩溃java.lang.IllegalStateException

时间:2012-10-10 15:06:45

标签: android

10-10 20:37:19.576: ERROR/AndroidRuntime(7619): Uncaught handler: thread main exiting due to uncaught exception
10-10 20:37:19.586: ERROR/AndroidRuntime(7619): 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(2131296396, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.ListView.layoutChildren(ListView.java:1432)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.AbsListView.onLayout(AbsListView.java:1119)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.View.layout(View.java:6836)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1003)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1640)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.os.Looper.loop(Looper.java:205)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at android.app.ActivityThread.main(ActivityThread.java:4409)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at java.lang.reflect.Method.invoke(Method.java:521)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-10 20:37:19.586: ERROR/AndroidRuntime(7619):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

1 个答案:

答案 0 :(得分:2)

您将收到此异常,因为您正在从后台线程更改适配器的内容。请更改您的实现,以便您只从UI线程更新适配器。

例外本身提到了这一点:

  

java.lang.IllegalStateException:适配器的内容已更改,但ListView未收到通知。确保不从后台线程修改适配器的内容,而只是从UI线程修改。

这是一个关于正确使用ListView和适配器的好教程: http://www.vogella.com/articles/AndroidListView/article.html