如何查看Android的ANR(无响应)异常

来源:互联网 发布:鼠标一直点软件 编辑:程序博客网 时间:2024/06/06 05:52

在Android开发中,当程序发生异常时会抛出异常信息,可以通过如下代码来获取异常信息,然而在ANR时我们在Logcat中并看不到异常信息。

Thread.setDefaultUncaughtExceptionHandler(new Thread. UncaughtExceptionHandler() {public void uncaughtException(Thread t, Throwable e) {   System.out.println(t + " throws exception: " + e);} });

那么我们在遇到一个ANR异常时,该怎样去排查是哪里的错误呢。通过查看Logcat的打印日志,会发现在每次出现ANR时都能看到这两句:
I/art: Thread[2,tid=25238,WaitingInMainSignalCatcherLoop,Thread*=0xafa0e400,peer=0x12c2a080,"Signal Catcher"]: reacting to signal 3I/art: Wrote stack traces to '/data/anr/traces.txt'

在发生ANR时,系统会将异常信息写入到traces文件中,系统会记录异常的位置、CPU和内存当时的使用情况,通过查看日志基本就能判断问题所在。
接下来打开该文件,通过shell命令就可以了。
如使用cat的打开文件:
1,
adb shell
-->
cat /data/anr/traces.txt

2,adb shell "cat /data/anr/traces.txt" | less


0 0
原创粉丝点击