对App进行开发测试时,偶尔出现app崩溃的问题。日志如下:

10-25 18:44:52.935  15290-15290/com.zzq.cnblogs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.zzq.cnblogs, PID: 15290
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)]
at android.widget.ListView.layoutChildren(ListView.java:1555)
at android.widget.AbsListView.onLayout(AbsListView.java:2089)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14845)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2026)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1783)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.j

  其中

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.

  由异常的提示可以看出,在一个后台线程更新了UI界面,而UI界面必须由UI线程进行更新。然而,Android提供了几种更新UI线程的方法。下面用Handler的方式更新UI,做个简单的修改即可。更多关于UI线程更新的方法见:http://gqdy365.iteye.com/blog/2112471

  1、添加一个Handler属性

    private Handler handlerListView = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = (Bundle) msg.obj;
int refreshType = (int) bundle.get("refreshType");
LinkedList<Article> list = (LinkedList<Article>) bundle.get("article");
for (Article article : list){
if (REFRESH_TYPE_UP == refreshType){
listData.add(article);
}else if (REFRESH_TYPE_DOWN == refreshType){
listData.add(0, article);
}
} pullToRefreshView.onRefreshComplete();
adapter.notifyDataSetChanged();
}
};

  2、将直接更新UI内容的部分

  pullToRefreshView.onRefreshComplete();
  adapter.notifyDataSetChanged();

  改成

    Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putInt("refreshType", refreshType);
bundle.putSerializable("article", addList);
msg.obj = bundle;
handlerListView.sendMessage(msg);

  将修改的数据发给handler去处理,而不是直接操作listview的adapter。

  此外还可以考虑使用activity的runOnUiThread方法进行更新。

最新文章

  1. C#语法糖大汇总
  2. Mina、Netty、Twisted一起学(七):发布/订阅(Publish/Subscribe)
  3. C预处理和C库
  4. [BS-22] Objective-C中nil、Nil、NULL、NSNull的区别
  5. ASP.NET MVC统一异常处理
  6. 剑指 offer set 8 树的子结构
  7. mysql的登录密码带特殊符号登录不进去的问题
  8. poj3373Changing Digits(dp)
  9. 201521123033《Java程序设计》第7周学习总结
  10. .Net Core 通过依赖注入和动态加载程序集实现宿程序和接口实现类库完全解构
  11. FORM开发之说明性弹性域开发
  12. Git 分支模型
  13. dotnet中Stream、string及byte[]的相关操作
  14. 【Linux】Jenkins安装
  15. ps最最基础的文档
  16. DAY3(PYTHON)
  17. Redis闪退解决办法
  18. Java并发编程(五)-- Java内存模型补充
  19. 案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
  20. holiday

热门文章

  1. 【BZOJ 3224】普通平衡树 模板题
  2. Intrusion Analysis Learning
  3. JavaWeb:报错信息The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path
  4. POJ2677 Tour(DP+双调欧几里得旅行商问题)
  5. Hibernate检索策略(抓取策略)(Hibernate检索优化)
  6. block中防止循环引用的一个高大上的宏定义
  7. webform添加到webapi的支持
  8. WPF 窗口在右下角出现,识别分辨率
  9. redhat RHEL 5.5 下载地址
  10. C函数tolower,与toupper