Eri*_*hen 6 android android-intent uncaught-exception android-activity
我的目标是在我的Android程序崩溃时显示一些错误消息.
我在我的程序的应用程序(MyApplication)onCreate中注册了一个未捕获的异常处理程序:
Thread.setDefaultUncaughtExceptionHandler(new HelloUncaughtExceptionHandler());
Run Code Online (Sandbox Code Playgroud)
当发现异常时,我想为用户启动一个新活动来保存/提交错误.这就是我目前正在做的事情:
class HelloUncaughtExceptionHandler implements UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
Intent intent = new Intent(MyApplication.this,
BugReportActivity.class);
intent.putExtra("stacktrace", stacktrace);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个没有BugReportActivity出现的空白屏幕(如果我从一个活动开始它工作正常)."MyApplication.this"让我担心......是否真的无法从我的应用程序中启动任意活动?
谢谢大家.