如何避免ANR对话框应用程序未响应

编程入门 行业动态 更新时间:2024-10-24 06:37:51
本文介绍了如何避免ANR对话框应用程序未响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一款Android应用,该应用使用在线服务,我需要加载我的Fragments。基本上,我有一个菜单,每个按钮替换下面的碎片。当我点击一个按钮时,片段开始被替换,如果我同时按下了键盘上的另一个按钮,我得到应用程序不响应对话框,然后单击等待,并成功加载我的片段。有没有办法阻止这种情况发生?由于Web服务,我的应用程序总是需要几秒钟才能加载。我在Google上读到,我可以使用AsyncTask,并在DoOnBackground方法上完成加载。我不知道这是否有效,但我甚至无法尝试,因为它不会让我改变视图,它会抛出一个关于只有原始线程可以改变视图的错误..我需要该服务完成加载我的视图,所以我甚至不能使用AsyncTask做这个服务..我现在已经没有想法了!有一种方法可以做到这一点..我欣赏建议!

I'm working on an Android app, which uses an online service, which i need to load my Fragments. Basically, i have like 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. 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!

推荐答案

防止这种情况发生的方法?

是的:不要在主应用程序线程上花太长时间。使用 StrictMode 来识别可能的罪魁祸首(磁盘I / O,网络I / O),并使用Traceview找出你正在做的事情,可能需要很长时间。 / b>

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.

由于Web服务的原因,我的应用总是需要几秒钟才能加载。

这应该在后台线程中完成。

That should be done in a background thread.

我在Google上阅读过,使用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对话框应用程序未响应

本文发布于:2023-11-06 13:35:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   应用程序   ANR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!