应用程序在异常后继续重新启动

时间:2016-05-11 14:27:28

标签: android exception

我创建了一个名为AppContext的类,并将其注册在我的清单

 <application
    android:name=".application.AppContext"

在本课程中,我有以下代码:

 @Override
public void onCreate() {
    super.onCreate();
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
}

ExceptionHandler - 类包含以下代码:

@Override
public void uncaughtException(Thread thread, final Throwable ex) {
        Intent exceptionIntent = new Intent(context, ExceptionActivity.class);
        exceptionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
        context.startActivity(exceptionIntent);
        System.exit(1);

为了测试它,我在随机活动中创建了一个函数,它调用了

Log.d("", null);

并抛出NullpointerException

它有效,如果发生异常,则显示ExceptionActivity。但该应用程序在抛出异常后不断重启。在3次中的2次中,重新启动活动覆盖了ExceptionActivity。 我该怎样预防呢?如果发生异常,我希望用户手动重启应用程序。谢谢!

0 个答案:

没有答案
相关问题