ANR keyDispatchingTimedOut

纵然是瞬间 提交于 2019-12-06 05:48:22

ANR is well known issue. Make sure you do all the expensive operations in AsyncTask and do not overburden onCreate, onStart or onResume activities.

Look at Responsiveness for more info.

In particular, Activities should do as little as possible to set up in key life-cycle methods such as onCreate() and onResume(). Potentially long running operations such as network or database operations, or computationally expensive calculations such as resizing bitmaps should be done in a child thread (or in the case of databases operations, via an asynchronous request). However, this does not mean that your main thread should block while waiting for the child thread to complete — nor should you call Thread.wait() or Thread.sleep(). Instead of blocking while waiting for a child thread to complete, your main thread should provide a Handler for child threads to post back to upon completion. Designing your application in this way will allow your main thread to remain responsive to input and thus avoid ANR dialogs caused by the 5 second input event timeout. These same practices should be followed for any other threads that display UI, as they are also subject to the same timeouts.

I found a very specific cause for one of these ANR errors.

In one of my app's layout definitions I had set android:layout_alignRight="fieldX"

But then in the definition of fieldX I had android:visibility="gone".

So the display was trying to align fields to a field that was not visible.

By taking out the android:visibility line for the fieldX definition the app started to work correctly without hanging.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!