6

Possible Duplicate:
How to start activity from UncaughtExceptionHandler if this is main thread crashed?

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

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

Thread.setDefaultUncaughtExceptionHandler(new HelloUncaughtExceptionHandler());

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

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?

Thanks all.

Community
  • 1
  • 1
Eric Chen
  • 3,562
  • 7
  • 39
  • 58
  • If you want to collect the Exception data without prompting the user you could use http://www.bugsense.com or go for the ACRA plugin – PanosJee Jul 18 '11 at 13:35

0 Answers0