我的应用程序正在导致一个强制关闭某处,而不是在我的LogCat中使用通常(和非常翔实的)堆栈跟踪来获取FATAL EXCEPTION,我只接收以下4行:
06-27 07:08:54.546: D/dalvikvm(14351): GC_FOR_MALLOC freed 9923 objects / 657416 bytes in 21ms 06-27 07:08:54.769: W/dalvikvm(14351): threadid=20: thread exiting with uncaught exception (group=0x4001d7f0) 06-27 07:08:54.796: W/dalvikvm(14351): threadid=21: thread exiting with uncaught exception (group=0x4001d7f0) 06-27 07:08:54.796: I/Process(14351): Sending signal. PID: 14351 SIG: 9
这是在DEBUG模式,没有过滤器应用于LogCat!
什么可能导致这种行为?
有没有办法告诉是什么导致这个例外?
更新:感谢@assylias下面,我已经能够实现:
final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread paramThread,Throwable paramThrowable) { Log.getStackTraceString(paramThrowable); subclass.uncaughtException(paramThread,paramThrowable); } });
哪些产生这些添加行:
06-27 08:24:47.105: D/dalvikvm(15475): GC_FOR_MALLOC freed 13865 objects / 1435952 bytes in 45ms 06-27 08:24:47.136: I/dalvikvm(15475): threadid=15: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.136: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x45209338 (56 left) 06-27 08:24:47.140: I/dalvikvm(15475): expanding stack end (0x45209300 to 0x45209000) 06-27 08:24:47.140: I/dalvikvm(15475): Shrank stack (to 0x45209300,curFrame is 0x4520937c) 06-27 08:24:47.159: I/dalvikvm(15475): threadid=16: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.159: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x4520c338 (56 left) 06-27 08:24:47.167: I/dalvikvm(15475): expanding stack end (0x4520c300 to 0x4520c000) 06-27 08:24:47.167: I/dalvikvm(15475): Shrank stack (to 0x4520c300,curFrame is 0x4520c37c) 06-27 08:24:47.175: I/dalvikvm(15475): threadid=17: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI 06-27 08:24:47.175: I/dalvikvm(15475): method requires 28+20+20=68 bytes,fp is 0x4520f338 (56 left) 06-27 08:24:47.175: I/dalvikvm(15475): expanding stack end (0x4520f300 to 0x4520f000) 06-27 08:24:47.175: I/dalvikvm(15475): Shrank stack (to 0x4520f300,curFrame is 0x4520f37c)
这当然是更有用的信息,但是现在我正在努力解决以下问题:
>应用程序现在不强制关闭,尽管调用了subclass.uncaughtException().为什么?
>所有这些堆栈溢出的意思是什么?我可以做什么在我糟糕的Android测试设备上如此征税?
>我如何知道我代码中的哪一部分导致这个?
更新:Log.getStackTraceString(paramThrowable);实际上没有打印任何东西.我收到的额外的打印是从bogus子类.uncaughtException(paramThread,paramThrowable);记录完整堆栈跟踪的正确方法是使用Log.e(TAG,“uncaughtException”,throwable).
现在唯一的问题是如何重新抛出异常?只是做一个抛出paramThrowable?
回答我的最后一个问题:Eclipse不会让我扔没有周围的try / catch,这导致我明白,我想要的不是一个重新抛出,而是一个killProcess().问题解决了.
解决方法@H_403_31@
您可以在应用程序的开头设置一个默认的未捕获异常处理程序,并记录其中的一些数据(下面的示例使用java记录器,但易于转置到Android):
private static void setDefaultUncaughtExceptionHandler() {
try {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t,Throwable e) {
logger.error("Uncaught Exception detected in thread {}",t,e);
}
});
} catch (SecurityException e) {
logger.error("Could not set the Default Uncaught Exception Handler",e);
}
}
private static void setDefaultUncaughtExceptionHandler() { try { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t,Throwable e) { logger.error("Uncaught Exception detected in thread {}",t,e); } }); } catch (SecurityException e) { logger.error("Could not set the Default Uncaught Exception Handler",e); } }