Android app main thread UI update intermittent crash issue

Source: Internet
Author: User

Occasional app crashes when developing tests on apps. The logs are as follows:

10-25 18:44:52.935 15290-15290/com.zzq.cnblogs e/androidruntime:fatal exception:main Process:com.zzq.cnblogs, PID:15290Java.lang.IllegalStateException:The content of the adapter have changed but ListView do not receive a notificatio N. Make sure the content of your adapter are not modified from a background thread, and only from the UI thread. Make sure your adapter calls notifydatasetchanged () while its content changes. [In ListView (16908298,classCOM.HANDMARK.PULLTOREFRESH.LIBRARY.PULLTOREFRESHLISTVIEW$INTERNALLISTVIEWSDK9) with Adapter (classAndroid.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

which

Make sure the content of your adapter are not modified from a background thread, but only from the UI thread.

The hint of the exception shows that the UI interface has been updated in a background thread, and the UI interface must be updated by the UI thread. However, Android provides several ways to update the UI thread. The following is a handler way to update the UI, make a simple change. More about UI thread updates see: http://gqdy365.iteye.com/blog/2112471

1. Add a Handler property

    PrivateHandler Handlerlistview =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Bundle Bundle=(Bundle) msg.obj; intRefreshtype = (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. The part of the UI content will be updated directly

Pulltorefreshview.onrefreshcomplete (); Adapter.notifydatasetchanged ();

Change into

    New Message ();     New Bundle ();    Bundle.putint ("Refreshtype", Refreshtype);    Bundle.putserializable ("article", addlist);     = bundle;    Handlerlistview.sendmessage (msg);

Send the modified data to handler instead of directly manipulating the adapter of the ListView.

You can also consider updating using the Runonuithread method of activity.

Android app main thread UI update intermittent crash issue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.