6

My application uses Google Analytics to track exceptions and crashes (among other thigs). I use this function to get the stacktrace:

public static void sendErrorReportViaGoogleAnalytics(Exception e) {

    e.printStackTrace();
    Tracker myTracker = EasyTracker.getTracker();
    myTracker.sendException(getDescription(e), false);
}

public static String getDescription(Exception t) {

    final StringBuilder result = new StringBuilder();
    result.append(t.toString());
    result.append(',');
    String oneElement;

    for (StackTraceElement element : t.getStackTrace()) {
        oneElement = element.toString();
        result.append(oneElement);
        result.append(",");
    }

    return result.toString();
}

This works fine, when talking about exceptions, I just call sendErrorReportViaGoogleAnalytics() in the catch part of my exception handling codes, but when it comes to crashes, I only get one line of the stacktrace, like

Binary XML file line #11: Error inflating class fragment

I set

<bool name="ga_reportUncaughtExceptions">true</bool>

in analytics.xml, as I'm using EasyTracker.

What should I do to get the full stacktrace in case of crashes as well?

4

2 回答 2

4

刚遇到这个问题。只需将给定的代码粘贴到项目的 BaseApplication.onCreate() 方法或其他位置,即可为未捕获的异常设置自定义 ExceptionReporter。不要忘记在analytics.xml 中声明给定的标志。

<bool name="ga_reportUncaughtExceptions">true</bool>

自定义未捕获异常报告器:

ExceptionReporter myHandler =
        new ExceptionReporter(EasyTracker.getInstance(this), GAServiceManager.getInstance(),
                              Thread.getDefaultUncaughtExceptionHandler(), this);

    StandardExceptionParser exceptionParser =
            new StandardExceptionParser(getApplicationContext(), null) {
                    @Override
                    public String getDescription(String threadName, Throwable t) {
                        return "{" + threadName + "} " + Log.getStackTraceString(t);
                    }
                };

    myHandler.setExceptionParser(exceptionParser);

    // Make myHandler the new default uncaught exception handler.
    Thread.setDefaultUncaughtExceptionHandler(myHandler);
于 2014-03-05T12:11:58.060 回答
3

由于您没有描述您实际为捕获崩溃所做的工作,因此我只能将您发送到文档: https ://developers.google.com/analytics/devguides/collection/android/v2/exceptions

如果您使用 EasyTracker,您可以声明:

<bool name="ga_reportUncaughtExceptions">true</bool>

否则,您可以按照描述实现 ExceptionReporter 类并将其附加到您的线程。

于 2013-04-18T11:15:49.480 回答