如何避免 ANR 对话框应用程序无响应 [英] How to avoid the ANR dialog Application Not Responding

查看:17
本文介绍了如何避免 ANR 对话框应用程序无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用在线服务的 Android 应用程序,我需要加载我的 Fragments.基本上,我有一个菜单,每个按钮替换下面的一个 Fragment.当我点击一个按钮时,片段开始被替换,如果我同时按下键盘上的另一个按钮,我会看到应用程序无响应对话框,然后我点击等待,我的片段加载成功地.

I'm working on an Android app, which uses an online service, which I need to load my Fragments. Basically, I have a menu, and each button replaces a Fragment underneath. When I click a button, the fragment starts being replaced, and if I press another button from my keyboard on the meantime, I get the Application Not Responding dialog, and then I click Wait, and my Fragment loads successfully.

有没有办法防止这种情况发生?由于网络服务,我的应用程序总是需要几秒钟才能加载.我在谷歌上读到,我可以使用 AsyncTask,并在 doOnBackground 方法上完成加载..我不知道这是否有效,但我什至不能试试吧,因为它不会让我改变那里的视图,它会抛出一个关于只有原始线程可以改变视图的错误.而且我需要该服务来完成我的视图加载,所以我什至无法使用 AsyncTask 来完成该服务.

Is there a way to prevent that from happening? My app will always take a few seconds to load, because of the web service. 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. And I need the service to finish loading my view, so I can't even do the service with the AsyncTask.

我现在没有想法了!必须有办法做到这一点.我很欣赏建议!

I'm running out of ideas now! there has to be a way to do this. I appreciate suggestions!

推荐答案

有没有办法防止这种情况发生?

Is there a way to prevent that from happening?

是的:不要在主应用程序线程上花费太长时间.使用 StrictMode 确定可能的罪魁祸首(磁盘 I/O、网络 I/O),并使用 Traceview 找出您正在执行的其他操作可能需要太长时间.

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.

我在谷歌上读到,我可以使用 AsyncTask,并完成 DoOnBackground 方法的加载..我不知道这是否有效,但我什至无法尝试,因为它不会让我改变查看那里,它会抛出一个关于只有原始线程可以更改视图的错误

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

这就是您从 onPostExecute() 方法更新视图的原因,如 AsyncTask 的文档.

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

这篇关于如何避免 ANR 对话框应用程序无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆