如何从throwable中捕获异常的原因?

时间:2016-07-21 00:03:28

标签: android stack-trace throwable

我有以下代码可以使用google analytics跟踪未捕获的异常:

ExceptionReporter myHandler =
            new ExceptionReporter(AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP),
                    Thread.getDefaultUncaughtExceptionHandler(), this);

    ExceptionParser ep = new ExceptionParser() {
        @Override
        public String getDescription(String s, Throwable throwable) {
            return "UserID:" + userID + " Version:" + versionName + " Exception:" + stringifyStacktrace(null, throwable);
        }
    };

private String stringifyStacktrace (Exception e, Throwable t){
    if(e != null) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    } else {
        return t.toString() + Arrays.toString(t.getStackTrace());
    }
}

问题是我只得到以下内容:

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.jiyofit.basic_app/in.jiyofit.basic_app.WorkoutActivity}: java.lang.ArithmeticException: divide by zero
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我想要堆栈跟踪的其余部分给出异常的行原因。我没有得到stacktrace的以下部分:

Caused by: java.lang.ArithmeticException: divide by zero
                                                                    at in.jiyofit.basic_app.WorkoutActivity.onCreate(WorkoutActivity.java:79)
                                                                    at android.app.Activity.performCreate(Activity.java:6251)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:148) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                    at java.lang.reflect.Method.invoke(Native Method)

如何获得这个?

1 个答案:

答案 0 :(得分:0)

从Log类

中尝试静态方法getStackTraceString()