Android app主线程UI更新间歇性崩溃的问题

  对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方法进行更新。

时间: 2024-12-10 01:50:24

Android app主线程UI更新间歇性崩溃的问题的相关文章

Android App在线程中创建handler的方法讲解_Android

相关概念1.Handler:可以看做是一个工具类,用来向消息队列中插入消息的; 2.Thread:所有与Handler相关的功能都是与Thread密不可分的,Handler会与创建时所在的线程绑定; 3.Message:消息; 4.MessageQueue:消息队列,对消息进行管理,实现了一个Message链表; 5.Looper:消息循环,从MessageQueue中取出Message进行处理: 6.HandlerThread:继承Thread,实例化时自动创建Looper对象,实现一个消息循

Android App在线程中创建handler的方法讲解

相关概念 1.Handler:可以看做是一个工具类,用来向消息队列中插入消息的; 2.Thread:所有与Handler相关的功能都是与Thread密不可分的,Handler会与创建时所在的线程绑定; 3.Message:消息; 4.MessageQueue:消息队列,对消息进行管理,实现了一个Message链表; 5.Looper:消息循环,从MessageQueue中取出Message进行处理: 6.HandlerThread:继承Thread,实例化时自动创建Looper对象,实现一个消息

有关Android中Service实现UI更新(Binder的运用)

我们知道Android的程序架构本身即是遵循MVC模式设计的,将显示和逻辑操作进行了很好的分离.xml文件进行view的添加和布局,Activity来实现各种View的展示,而service实现将数据按一定逻辑在View中显示.基于这样的原则我们设计程序时,就需要做到让他们各司其职,合理搭配,如此才能使我们设计的Android程序更高效,更安全以及易于维护,当然这是一个很大很大很大的话题,此处我只对service和Activity的职责进行简单阐述,希望能起到抛砖引玉的作用,当然若内容如有雷同不

Android 在其他线程中更新UI线程的解决方法_Android

方法一:Activity.runOnUiThread(Runnable )(经验之道: 这个最好用, 凡是要刷新页面的地方,Activity.runOnUiThread( new Runnable()  { public void run(){更新UI}}); 方法二:子线程调用Handler的sendMessage(message)发送事件. 复制代码 代码如下: mHandler = new Handler() {     @Override     public void handleMe

高效开发Android App的10个建议

中介交易 SEO诊断 淘宝客 云主机 技术大厅 假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗电或者内存占用等影响产品效率的每一个问题都会影响App的成功.这就是为什么在开发中确保最优化.运行流畅而且不会使Android系统出问题是至关重要的了.这里不需要讨论高效编程,因为我们不会关心你写的代码是否能够经得起测试.即使高效的代码也是需要时间来运行.今天这篇文章

Android实现在子线程中更新Activity中UI的方法_Android

本文实例讲述了Android实现在子线程中更新Activity中UI的方法.分享给大家供大家参考,具体如下: 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示.但是,在主线线程之外的线程中直接更新页面显示的问题是:系统会报这个异常: ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original

Android实现在子线程中更新Activity中UI的方法

本文实例讲述了Android实现在子线程中更新Activity中UI的方法.分享给大家供大家参考,具体如下: 在Android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示.但是,在主线线程之外的线程中直接更新页面显示的问题是:系统会报这个异常: ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original

android使用handler ui线程和子线程通讯更新ui示例_Android

复制代码 代码如下: package com.act262.sockettx; import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import and

android使用handler ui线程和子线程通讯更新ui示例

复制代码 代码如下:package com.act262.sockettx; import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import andr