错误信息
12-16 15:33:59.338 27587-27587/? E/InputEventReceiver: Exception dispatching input event.
12-16 15:33:59.338 27587-27587/? E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-16 15:33:59.359 27587-27587/? 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(2131165248, class com.whf.demo.TestListView) with Adapter(class com.whf.demo.ListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1620)
at android.widget.AbsListView.onTouchUp(AbsListView.java:4217)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3986)
at android.view.View.dispatchTouchEvent(View.java:10038)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2347)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:986)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2672)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2361)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2672)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2361)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2672)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2361)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2672)
... ...
错误来源
//ListView.java
@Override
protected void layoutChildren() {
... ...
if (mItemCount == 0) {
resetList();
invokeOnItemScrollListener();
return;
} else if (mItemCount != mAdapter.getCount()) {
throw new 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(" + getId() + ", " + getClass()
+ ") with Adapter(" + mAdapter.getClass() + ")]");
}
... ...
}
错误分析
- 确保Adapter的数据更新后一定要调用notifyDataSetChanged()方法通知ListView
- 数据更新和notifyDataSetChanged()放在UI线程内,且必须同步顺序执行,不可异步
- 仔细检查确认getCount()方法返回值是否正确
容错处理
//重写ListView (如果上述方案还未解决则选择该容错机制,否则不推荐)
@Override
protected void layoutChildren() {
try {
super.layoutChildren();
} catch (IllegalStateException e) {
Log.e("TestDemo", "SlideItemListView item count not equal notificationAdapter.getCount()");
((ListAdapter)getAdapter()).notifyDataSetChanged();
}
}
网友评论