How to avoid the ANR dialog Application Not Responding

試著忘記壹切 提交于 2019-11-30 16:34:23

Is there a way to prevent that from happening?

Yes: don't take so long on the main application thread. Use StrictMode to identify likely culprits (disk I/O, network I/O), and use Traceview to find out what else you are doing that might be taking too long.

My app will always take a few seconds to load, because of the web service.

That should be done in a background thread.

I read on Google, that i could use AsyncTask, and finish the loading on the DoOnBackground method.. I don't know if that works, but I can't even try, because it won't let me change the view there, it throws an error about only the original thread can change the views

Which is why you update the views from the onPostExecute() method, as is described in the documentation for AsyncTask.

You have to do changes to the ui in the onPostExecute() function of your AsyncTask.

Never execute a database operation or content provider operation in the context of the main thred. sooner or later you will be running into application Not Responding errors. the ANRs are not a good practice.

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