如何在android中获得完整的堆栈跟踪?
我正在使用以下代码如何在android中获得完整的堆栈跟踪?,android,google-analytics,Android,Google Analytics,我正在使用以下代码 public static String getCombinedStackTrace(Throwable aThrowable) { final StringBuilder result = new StringBuilder(); result.append(aThrowable.toString()); result.append(','); String oneEleme
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)
我无法获取“由以下原因引起的:”行如果您的垃圾有原因,可以通过调用
Throwable cause = aThrowable.getCause();
然后你可以递归地打电话
getCombinedStackTrace(cause);
并浓缩结果以获得完整的堆栈跟踪。您可以使用以下代码:
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();
谢谢。同样的问题,老板。它显示到“at dalvik.system.NativeStart.main(本机方法)”,但是“起因”没有得到解决