为什么 context.startActivity(intent) 不启动活动以及如何处理 android 中的异常?

Aka*_*ain 6 java android android-intent android-activity android-studio

我有一个处理来自 Activity 类的异常的异常处理程序,异常处理程序如下所示。

public class ExceptionHandler implements Thread.UncaughtExceptionHandler {
    public static final String TAG = "Exception handler";
    private final Context activity;

    public ExceptionHandler(Context activity) {
        this.activity = activity;
    }

    @Override
    public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
        Intent error = new Intent(activity, ErrorCatcher.class);
        activity.startActivity(error);
    }
}
Run Code Online (Sandbox Code Playgroud)

它是从活动类初始化的

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onRestart: Hey just created");
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));
// other methods and function
}
Run Code Online (Sandbox Code Playgroud)

当控件进入异常处理程序时,不会创建活动,而不是挂起我的应用程序的空白页面。

我得到的唯一消息是

I/Timeline: Timeline: Activity_launch_request time:417281208 intent:Intent { cmp=com.yyy.xxx/com.yyy.xxx.Activities.Error }
Run Code Online (Sandbox Code Playgroud)

编辑:好的,经过一些挖掘,我发现,如果没有抛出异常,则开始活动(即我可以看到活动页面),但是当抛出异常时显示空白页面。为什么这是仅在抛出异常时的条件以及在 android 中处理异常的更好方法是什么?有什么帮助吗?


编辑 2:它类似于这个https://codecrunch.co/blog/custom-error-handling-in-android/

Leo*_*oes 3

在startActivity之前添加error.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);. 这里还有更完整的答案