活动启动一半时间没有响应
这里有一些奇怪的事情:我的 Activity 在启动时有一半时间冻结,并且我从系统获取了一个 ARN。
不久前,我在 AndEngine +PhysicsBox2d 扩展中编写了一个简单的游戏,没有出现任何 ANR 问题。 我花了几天时间重构代码,引入了几个类来处理启动配置并移动代码以提高内聚性。 理想情况下,行为不会发生改变。 但是现在,如果我打开应用程序并正常关闭它,第二次打开它时,应用程序会冻结并强制关闭超时。 以下是日志的输出:
09-03 19:41:40.416: ERROR/ActivityManager(138): ANR in mp.andreabarbadoro.mama (mp.andreabarbadoro.mama/.DaisyGameActivity)
09-03 19:41:40.416: ERROR/ActivityManager(138): Reason: keyDispatchingTimedOut
09-03 19:41:40.416: ERROR/ActivityManager(138): Load: 2.77 / 2.62 / 2.35
09-03 19:41:40.416: ERROR/ActivityManager(138): CPU usage from 13439ms to 0ms ago with 99% awake:
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.2% 116/akmd: 0.9% user + 2.3% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 2% 1679/mp.andreabarbadoro.mama: 2% user + 0% kernel / faults: 3 minor
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.6% 138/system_server: 1% user + 0.5% kernel / faults: 3 minor 1 major
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.5% 228/com.android.launcher: 0.5% user + 0% kernel / faults: 57 minor
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.6% 1697/kworker/0:1: 0% user + 0.6% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0% 67/yaffs-bg-1: 0% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0% 1699/logcat: 0% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.4% TOTAL: 1.2% user + 0.2% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): CPU usage from 1542ms to 2104ms later:
09-03 19:41:40.416: ERROR/ActivityManager(138): 10% 138/system_server: 3.5% user + 7.1% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 173/InputDispatcher: 3.5% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.7% 149/SensorService: 1.7% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 116/akmd: 0% user + 3.5% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1694/akmd: 0% user + 3.5% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1679/mp.andreabarbadoro.mama: 3.5% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1688/er$SensorThread: 1.7% user + 1.7% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.9% 1697/kworker/0:1: 0% user + 0.9% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 10% TOTAL: 7.1% user + 3.5% kernel
09-03 19:41:50.756: WARN/ActivityManager(138): Force finishing activity mp.andreabarbadoro.mama/.DaisyGameActivity
那么,知道从哪里开始寻找这个错误吗?
编辑: 在调试窗口中查找 Activity.onDestroy 方法的断点后,我可以看到活动终止后 android.hardware.sensormanager$SensorThread 似乎仍然存在。 这是正常行为吗?
Something strange here: my Activity half of the time it is launched freezes and i get an ARN from the system.
A while ago i wrote i simple game in AndEngine + PhysicsBox2d extension, without incurring in any ANR problem.
I spent last few days refactoring the code, introducing a couple of classes to handle start-up configuration and moving code to improve cohesion.
Ideally no change in behaviour was implemented.
however now if i open the app and close it normally, the second time I open it, the app freeze and get a force close for time out.
Here is the output from the log:
09-03 19:41:40.416: ERROR/ActivityManager(138): ANR in mp.andreabarbadoro.mama (mp.andreabarbadoro.mama/.DaisyGameActivity)
09-03 19:41:40.416: ERROR/ActivityManager(138): Reason: keyDispatchingTimedOut
09-03 19:41:40.416: ERROR/ActivityManager(138): Load: 2.77 / 2.62 / 2.35
09-03 19:41:40.416: ERROR/ActivityManager(138): CPU usage from 13439ms to 0ms ago with 99% awake:
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.2% 116/akmd: 0.9% user + 2.3% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 2% 1679/mp.andreabarbadoro.mama: 2% user + 0% kernel / faults: 3 minor
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.6% 138/system_server: 1% user + 0.5% kernel / faults: 3 minor 1 major
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.5% 228/com.android.launcher: 0.5% user + 0% kernel / faults: 57 minor
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.6% 1697/kworker/0:1: 0% user + 0.6% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0% 67/yaffs-bg-1: 0% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0% 1699/logcat: 0% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.4% TOTAL: 1.2% user + 0.2% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): CPU usage from 1542ms to 2104ms later:
09-03 19:41:40.416: ERROR/ActivityManager(138): 10% 138/system_server: 3.5% user + 7.1% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 173/InputDispatcher: 3.5% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 1.7% 149/SensorService: 1.7% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 116/akmd: 0% user + 3.5% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1694/akmd: 0% user + 3.5% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1679/mp.andreabarbadoro.mama: 3.5% user + 0% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 3.5% 1688/er$SensorThread: 1.7% user + 1.7% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 0.9% 1697/kworker/0:1: 0% user + 0.9% kernel
09-03 19:41:40.416: ERROR/ActivityManager(138): 10% TOTAL: 7.1% user + 3.5% kernel
09-03 19:41:50.756: WARN/ActivityManager(138): Force finishing activity mp.andreabarbadoro.mama/.DaisyGameActivity
So, any idea where to start to look for this bug?
edit:
after looking in the debug window for Activity.onDestroy method's breakpoint, i can see how after the activity dies android.hardware.sensormanager$SensorThread seem to bo still alive.
Is this the normal behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以查看 /data/anr/traces.txt 并参考主线程的堆栈跟踪。 (假设该文件没有被新的 ANR 覆盖)。
You could look at /data/anr/traces.txt and refer to the main thread's stack trace. (assuming this file wasn't overwritten with new ANRs).
你在 onCreate 方法上做什么?看来你正在做一些繁重的事情,应该在不同的线程中处理。
如果没有看到代码,就很难指出问题所在......所以我现在最好的建议是为繁重的任务启动一个线程。
考虑显示启动屏幕/进度对话框,直到线程完成(您可以使用 AsyncTask 来实现)
What are you doing on the onCreate method? It seems that you're doing some heavy stuff that should be handled in a different thread.
Without seeing the code it would be extremely hard to point you to the problem... So my best suggestion for now is to start a thread for the heavy tasks.
Consider showing a splash screen / Progress dialog until the thread finishes (you can use AsyncTask for that)
请记住 ANR 的含义:您正在阻塞主/UI 线程一段时间(5 秒,请参阅 什么会触发ANR?),从而导致输入事件无法正确处理。
因此,您可能正在该线程内执行一些繁重的工作,可能是从
onResume()
或onRestart()
执行的。开始寻找这些昂贵的操作,并确保在单独的线程中执行它们(例如通过异步任务
)。如果您不确定应用中哪些操作成本较高,请考虑使用traceview
进行测量。请参阅 Android 开发者博客上的这篇文章,了解精彩内容介绍。Remember what the ANR means: You are blocking the main/UI-thread for a certain time (5s, see What Triggers ANR?), so that input events can't be handled correctly.
So you are probably doing some heavy work inside this thread, maybe executed from from
onResume()
oronRestart()
. Start looking for these expensive operations and make sure you execute them in a seperate thread (e.g. viaAsyncTasks
). If you are not sure what is an expensive operation in your app, consider measuring withtraceview
. See this post on the android developers blog for a nice introduction.