关于Listview的一个没有notifyDataSetChanged导致的错误
2016-08-15 10:12
1106 查看
log为:
这个方法内部调用了mDataSetObservable
可以观察到,notifyChanged最终还是调用了mObservers.get(i).onChanged()这是抽象类DataSetObserver中的一个方法。这个方法通知了listview的数据源发生了改变。
这个方法我们知道对于listview的操作中,如果改变了其adapter的数据内容,是需要去notifyDataSetChanged的,有时你的显示操作改变了adapter,对于一般开发者而言是不会忘记notifyDataSetChanged,并且我们注意到为listview重新设置adapter时也会间接调用onchangd()。
那么为何会导致文章开头的错误呢?原来是因为在fargement中使用了listview导致fragement切换时,被destroy,而导致listview被detachwindow,导致了错误的发生。
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(16908298, class com.handmark.pulltorefresh.library.PullToRefreshListView$InternalListViewSDK9) with Adapter(class android.widget.HeaderViewListAdapter)]错误总结
notifyDataSetChanged public void notifyDataSetChanged() { mDataSetObservable.notifyChanged(); }
这个方法内部调用了mDataSetObservable
public void notifyChanged() { synchronized(mObservers) { // since onChanged() is implemented by the app, it could do anything, including // removing itself from {@link mObservers} - and that could cause problems if // an iterator is used on the ArrayList {@link mObservers}. // to avoid such problems, just march thru the list in the reverse order. for (int i = mObservers.size() - 1; i >= 0; i--) { mObservers.get(i).onChanged(); } } }
可以观察到,notifyChanged最终还是调用了mObservers.get(i).onChanged()这是抽象类DataSetObserver中的一个方法。这个方法通知了listview的数据源发生了改变。
这个方法我们知道对于listview的操作中,如果改变了其adapter的数据内容,是需要去notifyDataSetChanged的,有时你的显示操作改变了adapter,对于一般开发者而言是不会忘记notifyDataSetChanged,并且我们注意到为listview重新设置adapter时也会间接调用onchangd()。
那么为何会导致文章开头的错误呢?原来是因为在fargement中使用了listview导致fragement切换时,被destroy,而导致listview被detachwindow,导致了错误的发生。
// AbsListView @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); mIsDetaching = true; // Dismiss the popup in case onSaveInstanceState() was not invoked dismissPopup(); // Detach any view left in the scrap heap mRecycler.clear(); final ViewTreeObserver treeObserver = getViewTreeObserver(); treeObserver.removeOnTouchModeChangeListener(this); if (mTextFilterEnabled && mPopup != null) { treeObserver.removeOnGlobalLayoutListener(this); mGlobalLayoutListenerAddedFilter = false; } if (mAdapter != null && mDataSetObserver != null) { mAdapter.unregisterDataSetObserver(mDataSetObserver); mDataSetObserver = null; }
相关文章推荐
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
- 关于安卓listview的一个简单错误Attempt to invoke virtual method
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
- 关于Ubuntu 使用PPPoe拨号上网,导致wifi没有错误。
- 一个关于“OLE DB 提供程序 'sqloledb' 指出该对象中没有任何列”错误的解决方法
- 关于在应用程序级别之外使用注册为allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。
- 因为错误消息指示这是由于上一个问题导致的错误,没有写入 apport 报告。
- 排查一个关于Parsley配置错误导致的问题
- java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误
- 一个关于“OLE DB 提供程序 'sqloledb' 指出该对象中没有任何列”错误的解决方法
- 最近做毕业设计发现市场上的Delphi书中关于TArrayField的一个错误理解,这里现简单的提一下以后有时间在好好的研究一下
- 《ASP.NET高级编程》中关于跨页传送的一个错误
- 在一个.net sln中包含多个project,project引用同一个dll导致的错误
- 在一个.net sln中包含多个project,project引用同一个dll导致的错误
- 关于一个最终没有投入使用的网站
- 头文件被修改导致一个难以理解、难以调试的错误
- 关于《一个SQL语句查询问题(查询最小值)(急)》回复里面没有正确理想的答案!
- 配置文件多个一个">"符号,导致struts抛出了匪夷所思的错误
- MSDN上关于sqlserver 万能分页原理实现的一个致命错误
- 一个男生写的关于怎么追女生昨天没有说一个很关键的东西