Android的:如何从UncaughtExceptionHandler的集应用程序的启动活动应用程序、Android、UncaughtExceptionHandler

2023-09-12 05:00:52 作者:煙ゝ刺痛了眼伤了肺

可能重复:   How从UncaughtExceptionHandler的启动活动,如果这是主线程崩溃?

我的目标是显示一些错误消息时,我的Andr​​oid程序崩溃。

My goal is to display some error messages when my Android program crashes.

我在我的程序申请注册的未捕获的异常处理程序(所有MyApplication)的onCreate:

I have an uncaught exception handler registered in my program's application (MyApplication) onCreate:

Thread.setDefaultUncaughtExceptionHandler(new HelloUncaughtExceptionHandler());

当一个异常被抓住了,我想开始一个新的活动,为用户节省/提交Bug。这是我目前做的:

And when an exception is caught, I'd like to start a new activity for the user to save/submit bugs. This is what I am currently doing:

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);
    }
}

我越来越没有BugReportActivity一个空白的屏幕显示出来(如果我从它工作正常活动启动)。而MyApplication.this我担心......是真的没有办法从我的应用程序启动任意活动?

I am getting a blank screen without BugReportActivity showing up (if I start it from an activity it works fine). The "MyApplication.this" worries me... is there really no way to launch an arbitrary activity from my application?

谢谢大家。

推荐答案

虽然我不知道,我认为你应该使用的应用程序上下文,( MyApplication.this.getApplicationContext ),同时,你为什么不关闭的PrintWriter 之前调用 result.toString();

While I'm not sure, I think you should use the application context, (MyApplication.this.getApplicationContext), also, why don't you close the printWriter before calling result.toString(); ?

 
精彩推荐
图片推荐