2015-07-10 26 views
0

当我的应用程序崩溃,在抛出一些logcat中线路:如何强制android应用程序打印未捕获异常的堆栈跟踪?

07-10 10:35:34.671 32391-32391/com.noframe.farmisagronom D/AndroidRuntime﹕ Shutting down VM 
07-10 10:35:34.671 32391-32391/com.noframe.farmisagronom W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415bf8b0) 

如何强制应用程序打印异常堆栈跟踪?

我的应用程序类代码:

public class Application extends MultiDexApplication { 

    @Override 
    protected void attachBaseContext(Context base) { 
     try { 
      super.attachBaseContext(base); 
      MultiDex.install(this); 
     }catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onCreate() { 

     try { 
      Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { 

       @Override 
       public void uncaughtException(Thread t, Throwable e) { 
        Log.e("Uncaught Exception detected in thread {}", t.getName(), e); 
       } 
      }); 
     } catch (SecurityException e) { 
      Log.e("Could not set the Default Uncaught Exception Handler", "" ,e); 
     } 
     super.onCreate(); 
    } 
} 

注意setDefaultUncaughtExceptionHandler是我试图让堆栈跟踪形式的异常,但没有作用。

当我疯狂地尝试抓住每一个地方,我发现这些例外。但这并不总是一种选择,并且需要大量时间。

回答

0

您可以看到使用(String tag,String msg,Throwable tr)签名的所有日志方法Here's the link的重写。

传递异常作为第三个参数应该给你logcat中的完整堆栈跟踪。

这三个参数的日志方法使用getStackTraceString()方法

+0

你也可以看[这里](http://developer.android.com/reference/android/util/Log.html#getStackTraceString%28java.lang.Throwable%29),在这里你也可以看到解决方案[需要处理未捕获的异常并发送日志文件](http://stackoverflow.com/questions/19897628/need-to-handle-uncaught-exception-and-send-log-file) – Shekhar

+0

public static String getStackTraceString(Throwable tr)我假设?而且我得到了巫术魔法的Throwable或一些特殊的未捕获异常舞蹈? – Alpha

0

我没有这样说:

StackTraceElement[] st = new StackTraceElement[12]; 
st = ex.getStackTrace(); 
for (StackTraceElement el: st) 
    com.example.android.common.logger.Log.e(TAG, " " + el); 

希望这将有助于。