Android中的ANR异常及traces解析
ANR(Application Not Responding)定义
出现ANR的原因
三种常见类型
1:KeyDispatchTimeout(5 seconds) --主要类型
按键或触摸事件在特定时间内无响应
2:BroadcastTimeout(10 seconds)
BroadcastReceiver在特定时间内无法处理完成
3:ServiceTimeout(20 seconds) --小概率类型
Service在特定的时间内无法处理完成
如何分析ANR生成的Traces
DALVIK THREADS: (mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0) "main" prio=5 tid=1 NATIVE | group="main" sCount=1 dsCount=0 obj=0x40025340 self=0xd180 | sysTid=1071 nice=0 sched=0/0 cgrp=default handle=-1344994080 | schedstat=( 2355584448 1199910712 3410 ) at java.net.InetAddress.getaddrinfo(Native Method) at java.net.InetAddress.lookupHostByName(InetAddress.java:540) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:333) at java.net.InetAddress.getAllByName(InetAddress.java:295) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:100) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:79) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:353) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:120) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:316) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:298) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:236) at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:645) at com.rayray.cool.util.URLUtil.invokeURL(URLUtil.java:136) at com.rayray.cool.activity.WoDeJianYiActivity$1.onClick(WoDeJianYiActivity.java:173) at android.view.View.performClick(View.java:2535) at android.view.View$PerformClick.run(View.java:9129) at android.os.Handler.handleCallback(Handler.java:618) at android.os.Handler.dispatchMessage(Handler.java:123) at android.os.Looper.loop(SourceFile:351) at android.app.ActivityThread.main(ActivityThread.java:3821) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:538) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) at dalvik.system.NativeStart.main(Native Method)
出现ANR问题的原因就是上面红色代码中
at com.rayray.cool.activity.WoDeJianYiActivity$1.onClick(WoDeJianYiActivity.java:173) 是Android中Activity
at com.rayray.cool.util.URLUtil.invokeURL(URLUtil.java:136) 是一次耗时的联网请求
Activity中的按键事件在特定事件内未响应造成。
如何解决ANR
(1)耗时的操作放入单独的线程中处理,如(联网、数据库、IO)
(2)尽量避免和UI线程的操作
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。