Android的:如何从UncaughtExceptionHandler的集应用程序的启动活动 [英] Android: How to start an activity from an UncaughtExceptionHandler set in application

查看:163
本文介绍了Android的:如何从UncaughtExceptionHandler的集应用程序的启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/2222396/how-to-start-activity-from-uncaughtexceptionhandler-if-this-is-main-thread-crash">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(); ?

这篇关于Android的:如何从UncaughtExceptionHandler的集应用程序的启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆