我有一个应用程序,它没有在市场上(使用调试证书签名),但是我想在我的应用程序崩溃时获取崩溃日志数据。我在哪里可以找到关于应用程序崩溃原因的日志?
如果您的应用程序被其他人下载并在远程设备上崩溃,您可能需要查看一个Android错误报告库(在此SO帖子中引用)。 如果仅发生在本地设备上,则可以使用LogCat。
即使设备在崩溃发生时未连接到主机计算机,连接设备并发出adb logcat
命令也会下载整个logcat历史记录(至少缓冲区大小范围内,通常是大量日志数据,但不是无限的)。 这两个选项是否回答了您的问题? 如果没有,您可以尝试澄清一下您正在寻找什么。
您可以尝试从控制台执行以下操作:
adb logcat --buffer=crash
关于此选项的更多信息:
adb logcat --help
...
-b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',
'system', 'radio', 'events', 'crash', 'default' or 'all'.
Multiple -b parameters or comma separated list of buffers are
allowed. Buffers interleaved. Default -b main,system,crash.
Thread.UncaughtExceptionHandler
接口,并将其传递给Thread.setDefaultUncaughtExceptionHandler()
,在您的Activity的onCreate()
方法开头调用。以下是实现类TopExceptionHandler
的示例代码。public class TopExceptionHandler implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler defaultUEH;
private Activity app = null;
public TopExceptionHandler(Activity app) {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
this.app = app;
}
public void uncaughtException(Thread t, Throwable e) {
StackTraceElement[] arr = e.getStackTrace();
String report = e.toString()+"\n\n";
report += "--------- Stack trace ---------\n\n";
for (int i=0; i<arr.length; i++) {
report += " "+arr[i].toString()+"\n";
}
report += "-------------------------------\n\n";
// If the exception was thrown in a background thread inside
// AsyncTask, then the actual exception can be found with getCause
report += "--------- Cause ---------\n\n";
Throwable cause = e.getCause();
if(cause != null) {
report += cause.toString() + "\n\n";
arr = cause.getStackTrace();
for (int i=0; i<arr.length; i++) {
report += " "+arr[i].toString()+"\n";
}
}
report += "-------------------------------\n\n";
try {
FileOutputStream trace = app.openFileOutput("stack.trace",
Context.MODE_PRIVATE);
trace.write(report.getBytes());
trace.close();
} catch(IOException ioe) {
// ...
}
defaultUEH.uncaughtException(t, e);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));
...
这个处理程序将跟踪信息保存在文件中。当ReaderScope
下一次重新启动时,它会检测到该文件并提示用户是否要将其发送给开发人员。
要通过电子邮件发送堆栈跟踪,请执行以下代码将其打包到电子邮件中。
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(ReaderScopeActivity.this.openFileInput("stack.trace")));
while((line = reader.readLine()) != null) {
trace += line+"\n";
}
} catch(FileNotFoundException fnfe) {
// ...
} catch(IOException ioe) {
// ...
}
Intent sendIntent = new Intent(Intent.ACTION_SEND);
String subject = "Error report";
String body = "Mail this to appdeveloper@gmail.com: " + "\n" + trace + "\n";
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"readerscope@altcanvas.com"});
sendIntent.putExtra(Intent.EXTRA_TEXT, body);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.setType("message/rfc822");
ReaderScopeActivity.this.startActivity(Intent.createChooser(sendIntent, "Title:"));
ReaderScopeActivity.this.deleteFile("stack.trace");
您可以使用ACRA错误报告系统。只需在项目libs中包含ACRA.jar,并在启动器活动类声明之前使用以下代码片段:
@ReportsCrashes(formKey = "", mailTo = "abc@gmail.com;def@yahoo.com", mode = ReportingInteractionMode.SILENT)
或者您可以尝试从控制台执行以下操作:
adb logcat -b crash
这段内容来源于http://www.herongyang.com/Android/Debug-adb-logcat-Command-Debugging.html
你可以使用adb命令:
adb logcat AndroidRuntime:E *:S
如果您正在使用Eclipse,请确保使用调试而不是运行。 确保您处于调试视角(右上方) 您可能需要按下“Resume”(F8)几次才能打印日志。 崩溃日志将在底部的Logcat窗口中-双击全屏并确保滚动到底部。您将看到错误的红色文本,崩溃跟踪将类似于
09-04 21:35:15.228: ERROR/AndroidRuntime(778): Uncaught handler: thread main exiting due to uncaught exception
09-04 21:35:15.397: ERROR/AndroidRuntime(778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dazlious.android.helloworld/com.dazlious.android.helloworld.main}: java.lang.ArrayIndexOutOfBoundsException
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.os.Looper.loop(Looper.java:123)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread.main(ActivityThread.java:3948)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at dalvik.system.NativeStart.main(Native Method)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): Caused by: java.lang.ArrayIndexOutOfBoundsException
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at com.example.android.helloworld.main.onCreate(main.java:13)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
09-04 21:35:15.397: ERROR/AndroidRuntime(778): ... 11 more
09-04 21:35:15.397: ERROR/AndroidRuntime(778): Caused by: java.lang.ArrayIndexOutOfBoundsException
09-04 21:35:15.397: ERROR/AndroidRuntime(778): at com.example.android.helloworld.main.onCreate(main.java:13)
这些告诉我们,是在onCreate方法中main.java的第13行发生了数组越界异常。
这里有一个关于崩溃日志的解决方案。
Android市场有一个名为“Crash Collector”的工具。
请查看以下链接获取更多信息。
http://kpbird.blogspot.com/2011/08/android-application-crash-logs.html
以下是一个可以帮助您将所有日志转储到文本文件的解决方案:
adb logcat -d > logs.txt
adb logcat
即可。或者您可以使用包含在Eclipse插件中的SDK工具。 - Chris Thompson$SDK_DIR/platform-tools/
。显示错误的命令为:.\adb.exe logcat -v time *:E
。 - Harun