我有一个应用程序,它没有在市场上(使用调试证书签名),但是我想在我的应用程序崩溃时获取崩溃日志数据。我在哪里可以找到关于应用程序崩溃原因的日志?
1)通过USB插入手机(开启开发人员调试选项)
2)打开终端并导航到您的Android SDK目录(对于Mac):
cd ~/Library/Android/sdk/platform-tools
3)从该目录记录Logcat(在您的终端中)以生成日志流(对于Mac):
./adb logcat
4)打开崩溃的应用程序以生成崩溃日志
5)按Ctrl+C停止终端并查找与崩溃应用相关的日志。可能会显示类似以下内容:
AndroidRuntime:FATAL EXCEPTION: main
compile 'com.balsikandar.android:crashreporter:1.0.1'
本地设备上查找所有崩溃并在方便时进行修复。崩溃使用易于跟踪的日期和时间格式保存。此外,还提供了以下方法捕获已记录的异常的API。
CrashRepoter.logException(Exception e)
如果您只是想在手机连接到电脑时查看崩溃日志,请使用Eclipse中的DDMS视图,当您的应用程序在调试过程中崩溃时,报告就在DDMS中的LogCat中。
基于此POST,使用此类替换"TopExceptionHandler"
class TopExceptionHandler implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler defaultUEH;
private Activity app = null;
private String line;
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();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"kevineyni@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "crash report azar");
String body = "Mail this to kevineyni@gmail.com: " + "\n" + trace + "\n";
i.putExtra(Intent.EXTRA_TEXT , body);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
// Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
// ReaderScopeActivity.this.startActivity(Intent.createChooser(sendIntent, "Title:"));
//ReaderScopeActivity.this.deleteFile("stack.trace");
} catch(IOException ioe) {
// ...
}
defaultUEH.uncaughtException(t, e);
}
private void startActivity(Intent chooser) {
}
}
.....
在同一个Java类文件(Activity)中 ......Public class MainActivity.....
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new TopExceptionHandler(this));
.....