如何在Android中获得完整的堆栈跟踪?

时间:2013-06-07 12:53:49

标签: android google-analytics

我正在使用以下代码

public static String getCombinedStackTrace(Throwable aThrowable) {

            final StringBuilder result = new StringBuilder();
            result.append(aThrowable.toString());
            result.append(',');

            String oneElement;

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

它在“引起:”之前返回堆栈跟踪,但我想在之后得到“引起:”。 提前致谢

Caused by: java.sql.BatchUpdateException: failed batch
    at org.hsqldb.jdbc.jdbcStatement.executeBatch(jdbcStatement.java:1102)
    at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(jdbcPreparedStatement.java:514)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)

我无法得到“引起的:”行

2 个答案:

答案 0 :(得分:0)

如果您的Throwable有原因,可以通过调用

来检索
Throwable cause = aThrowable.getCause();

然后你可以递归调用

getCombinedStackTrace(cause);

并将结果汇​​总以获得完整的堆栈跟踪。

答案 1 :(得分:0)

您可以使用此代码:

import java.io.PrintWriter;
import java.io.StringWriter;

 Writer result = new StringWriter();
            PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            //String stacktrace = result.toString();
            //Report += stacktrace;
            printWriter.append( "\n"
                    + "Cause : \n"
                    + "======= \n");

            // If the exception was thrown in a background thread inside
            // AsyncTask, then the actual exception can be found with getCause
            Throwable cause = e.getCause();
            while (cause != null) {
                cause.printStackTrace(printWriter);
                cause = cause.getCause();
            }

            String trace = result.toString(); // here is the result!
            printWriter.close();