我的应用程序出现 ANR 问题

发布于 2024-10-30 13:08:33 字数 524 浏览 1 评论 0原文

我有一个相当繁重的活动,UI 线程中发生了很多事情,还有活动中的其他可运行线程。在此主要活动中,我通过按下按钮启动一个新活动,使用 startActivityForResult() 调用该按钮。现在,当我开始新活动时,我收到 ANR 错误,这意味着有些事情花费的时间超过了设定的时间,并且弹出了 ForceClose/Wait 对话框。

问题是,对于新的 Activity,我只设置该 Activity 的 contentView,而没有其他任何内容,因此这意味着前一个 Activity 的某些内容导致其挂起。直到最近它才在应用程序中从未出现过这种情况,我无法想象我添加了什么会导致这种情况。

在第一个 Activity 中,我有 OpenFeint、SurfaceHolder.Callback、后台线程中的数据库调用以及大量图像操作。这些进程中是否有任何一个进程的饥饿程度足以导致 ANR?

我是否可以在加载第二个活动时暂停第一个活动(我认为无论如何它都会这样做)?就像我说的,第二个 Activity 除了加载 ContentView 之外什么也不做。

I have quite a heavy Activity with a lot of things going on in the UI Thread, and also other Runnable threads within the Activity. Within this main activity I start a new Activity with a button press which is called using startActivityForResult(). Now when I start the new activity I get an ANR error meaning that there is something taking longer than the set amount of time and I get the ForceClose/Wait Dialog pop up.

The thing is, for the new Activity I am only setting the contentView for the activity and nothing else so it means something from the previous Activity is causing it to hang. It has never down this before in the app only recently and I can't think what I have added for it to cause this.

In the first Activity I have OpenFeint, SurfaceHolder.Callback, database calls in background thread and lot's of image manipulation. Are any of these processes hungry enough to cause the ANR?

Could I possibly put the first activity on hold while the second one loads (I thought it did that anyway)? Like I say the second Activity does nothing other than load a ContentView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我纯我任性 2024-11-06 13:08:33

当您开始一项新活动时,应暂停之前的活动,正如您所说。但首先会调用 Activity 的 onPause。也许就是这个方法导致了ANR?特别是当它保存大量状态数据时。

这里只是猜测。

When you start a new activity, the previous activity should be put on hold, as you say. But first the activity's onPause is called. Maybe it's this method which causes the ANR? Especially if it is saving large amounts of state data.

Just guessing here.

要走就滚别墨迹 2024-11-06 13:08:33

事实上,存在 ANR 很明显,UI 线程中的某些内容运行时间更长(正如您所指出的)。而且,你似乎做了很多事情(至少解释给人这种感觉)。此时我可以说尝试注释掉某些部分,看看问题是否仍然存在。例如,注释 startActivityForResult(),这应该告诉您哪个活动是罪魁祸首。另外,如果您可以提供一些伪代码,我相信人们会得到更好的想法并且能够提供更好的帮助。

The fact that there is an ANR it is pretty clear that something is running longer in the UI thread (as you have pointed out). Also, you seem to be doing a lot of things (at least the explanation gives this feeling). At this point I can say try commenting out certain parts and see if the problem persists. E.g. comment the startActivityForResult(), this should tell you which activity is the culprit. Also, if you can put up some pseudo code, I'm sure people would get a better idea and would be able to help better.

分分钟 2024-11-06 13:08:33

ANR 通常在您请求 UI 时发生(例如按下按钮),因此我认为导致 ANR 的不是第二个 Activity 启动,而是您使用 UI 的事实。

所以问题出在你的第一个活动上,而不是第二个活动上。

The ANR occurs usually when you solicit the UI (like pressing a button), so I think it's not the second activity launch which causes the ANR, but the fact you use the UI.

So the problem is from your first activity, not the second.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文