如何获取Android崩溃日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3643395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Android crash logs?
提问by Sheehan Alam
I have an app that is not in the market place (signed with a debug certificate), but would like to get crash log data, whenever my application crashes. Where can I find a log of why my app crashed?
我有一个不在市场上的应用程序(使用调试证书签名),但我想在我的应用程序崩溃时获取崩溃日志数据。我在哪里可以找到有关我的应用程序崩溃原因的日志?
采纳答案by Chris Thompson
If your app is being downloaded by other people and crashing on remote devices, you may want to look into an Android error reporting library (referenced in this SO post). If it's just on your own local device, you can use LogCat.
Even if the device wasn't connected to a host machine when the crash occurred, connecting the device and issuing an adb logcat
command will download the entire logcat history (at least to the extent that it is buffered which is usually a loooot of log data, it's just not infinite). Do either of those options answer your question? If not can you attempt to clarify what you're looking for a bit more?
如果您的应用被其他人下载并在远程设备上崩溃,您可能需要查看 Android 错误报告库(在此 SO 帖子中引用)。如果它只是在你自己的本地设备上,你可以使用LogCat.
即使崩溃发生时设备没有连接到主机,连接设备并发出adb logcat
命令将下载整个 logcat 历史记录(至少在某种程度上它被缓冲,这通常是日志数据的 loooot,它不是无限的)。这些选项中的任何一个都回答您的问题吗?如果没有,您能否尝试进一步澄清您正在寻找的内容?
回答by TanvirChowdhury
The way to do this is to implement the Thread.UncaughtExceptionHandler
interface and pass it to Thread.setDefaultUncaughtExceptionHandler()
at the beginning of your Activity's onCreate()
. Here is the implementation class TopExceptionHandler
.
这样做的方法是实现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);
}
}
Note We let the Android framework's defaultUEH to handle it.
注意 我们让 Android 框架的 defaultUEH 来处理它。
At the top of your Activity register an instance of above class like this:
在您的 Activity 顶部注册上述类的一个实例,如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));
...
This handler saves the trace in a file. When ReaderScope
restarts next time, it detects the file and prompts the user if he/she wants to email it to the developer.
此处理程序将跟踪保存在文件中。当ReaderScope
下次启动时,它检测到的文件和提示,如果他/她想要通过电子邮件发送给开发者用户。
To Email the Stack Trace, execute following code to pack it in an email.
要通过电子邮件发送堆栈跟踪,请执行以下代码将其打包到电子邮件中。
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 [email protected]: " + "\n" + trace + "\n";
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
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");
Or you can also use ACRA Error Reporting System.Just Include the ACRA.jar in your project libs and use the below code snippet before your launcher activity class declaration
或者您也可以使用 ACRA 错误报告系统。只需在您的项目库中包含 ACRA.jar 并在您的启动器活动类声明之前使用以下代码片段
@ReportsCrashes(formKey = "", mailTo = "[email protected];[email protected]", mode = ReportingInteractionMode.SILENT)
or You can try this from console:-
或者您可以从控制台尝试此操作:-
adb logcat -b crash
回答by Kanwar Malik
This is from http://www.herongyang.com/Android/Debug-adb-logcat-Command-Debugging.html
这是来自http://www.herongyang.com/Android/Debug-adb-logcat-Command-Debugging.html
You can use adb:
您可以使用亚行:
adb logcat AndroidRuntime:E *:S
回答by Ameer ali khan
You can try this from the console:
您可以从控制台尝试此操作:
adb logcat --buffer=crash
More info on this option:
有关此选项的更多信息:
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.
回答by T. Markle
If you're using Eclipse, make sure you use debug and not run. Make sure you are in the debug perspective (top right) You may have to hit 'Resume' (F8) a few times for the log to print. The crash log will be in the Logcat window at the bottom- double click for fullscreen and make sure you scroll to the bottom. You'll see red text for errors, the crash trace will be something like
如果您使用的是 Eclipse,请确保您使用的是调试而不是运行。确保您处于调试视角(右上角) 您可能需要多次点击“恢复”(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.access00(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
The important parts for this one are
这一个的重要部分是
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)
those tell us it was an array out of bounds exception on on line 13 of main.java in the onCrate method.
那些告诉我们它是 onCrate 方法中 main.java 的第 13 行上的数组越界异常。
回答by Jarek Potiuk
You can use Apphance. This is a cross-platform service (now mainly Android, iOS with other platforms on their way) which allows to debug remotely any mobile device (Android, iOS now - others under development). It's much more than just a crashlog, in fact it is much more: logging, reporting of problems by testers, crashlogs. It takes about 5 minutes to integrate. Currently you can request for access to closed beta.
您可以使用 Apphance。这是一项跨平台服务(现在主要是 Android、iOS 以及其他平台正在开发中),它允许远程调试任何移动设备(Android、iOS 现在 - 其他正在开发中)。它不仅仅是一个崩溃日志,实际上还有更多:日志记录、测试人员报告问题、崩溃日志。集成大约需要 5 分钟。目前,您可以请求访问封闭测试版。
Disclaimer: I am CTO of Polidea, a company behind Apphance and co-creator of it.
免责声明:我是 Polidea 的首席技术官,它是 Apphance 背后的公司和它的共同创建者。
Update: Apphance is no longer closed beta! Update 2: Apphance is available as part of http://applause.comoffering
更新:Apphance 不再是封闭测试版!更新 2:Apphance 作为http://applause.com产品的一部分提供
回答by Ketan Parmar
Here is another solution for Crash Log.
这是崩溃日志的另一种解决方案。
Android market has tool named "Crash Collector"
Android 市场有名为“崩溃收集器”的工具
check following link for more information
检查以下链接以获取更多信息
http://kpbird.blogspot.com/2011/08/android-application-crash-logs.html
http://kpbird.blogspot.com/2011/08/android-application-crash-logs.html
回答by Jesús Manzano
回答by Ariel Bell
If you are looking for a basic crash reporting tool, try crashlytics.
如果您正在寻找基本的崩溃报告工具,请尝试crashlytics。
If you want a more advanced reporting tool, Checkout Gryphonet. It logs all the crashes occured along with the exact line of code that caused the crash along with automated markers that show you the steps the user took prior to the crash and more.
如果您想要更高级的报告工具,请查看Gryphonet。它记录了发生的所有崩溃以及导致崩溃的确切代码行以及显示用户在崩溃前采取的步骤等的自动标记。
Good luck!
祝你好运!