用UncaughtExceptionHandler捕获异常,但显示崩溃对话框



我已经实现了一个ExceptionHandler来防止应用程序启动崩溃的活动,它的工作。我的问题是,它不再显示错误对话框。下面是ExceptionHandler的代码:

public class MyExceptionHandler implements
    java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;
public MyExceptionHandler(Context context, Class<?> c) {
    myContext = context;
    myActivityClass = c;
}
public void uncaughtException(Thread thread, Throwable exception) {
    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    System.err.println(stackTrace);// You can use LogCat too
    Intent intent = new Intent(myContext, myActivityClass);
    String s = stackTrace.toString();
    //you can use this String to know what caused the exception and in which Activity
    intent.putExtra("uncaughtException",
            "Exception is: " + stackTrace.toString());
    intent.putExtra("stacktrace", s);
    myContext.startActivity(intent);
    //for restarting the Activity
    Process.killProcess(Process.myPid());
    System.exit(0);
}
}

有没有人有一个想法,如何显示正常的黑色崩溃对话框,在重新启动应用程序之前?

提前感谢!

我找到了解决办法。问题是:

 Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
//you can use this String to know what caused the exception and in which Activity
intent.putExtra("uncaughtException",
        "Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);

删除startActivity,解决了对话框的问题,并且仍然按照预期重新启动了应用程序。

编辑:

对话框提示,但只是因为第二次崩溃发生,因为缺少数据,所以这个答案是不正确的…

相关内容

  • 没有找到相关文章