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

Why context.startActivity(intent) not starting the activity and how to handle exception in android?

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

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

它是从activityclass

初始化的
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onRestart: Hey just created");
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));
// other methods and function
}

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

我收到的唯一消息是

I/Timeline: Timeline: Activity_launch_request time:417281208 intent:Intent { cmp=com.yyy.xxx/com.yyy.xxx.Activities.Error }

编辑: 好的,经过一些挖掘我发现,如果没有抛出异常然后 activity 开始(即我可以看到 Activity page) 但是当抛出异常时显示空白页。为什么只有在抛出异常时才会出现这种情况,在 android 中处理异常的更好方法是什么?有帮助吗?


编辑 2:与此类似 https://codecrunch.co/blog/custom-error-handling-in-android/

在您覆盖的 uncaughtException 方法上,您最好使用此代码 activity 启动

   startActivity(activity,Error.class);

当我使用 try/catch 而不是

时它起作用了
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));

处理异常,可能不是最好的方法,但值得一试。

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

此代码可能对您有所帮助

Application application = (Application) context.getApplicationContext();
Intent intent = new Intent(application, ErrorActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
application.startActivity(intent);

如果没有再通知我,我会把异常处理的完整代码发给你!

首先,您正在使用 Application Context 来尝试启动 activity。这可能会导致您的一些问题。这是可以做到的,但我认为有一种更简单的方法可以实现所有这些。

还需要注意的是,你忘记了

里面的其余代码
  @Override
public void uncaughtException
    ....
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(10);

Why is this the condition for only when an exception is thrown...

这是因为你重写了uncaughtException方法。所以当你得到一个未捕获的异常时,你将开始 Error.class Activity.

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

...and what is a better way to handle exception in android??

有多种方法可以处理异常。这实际上取决于用例是什么。通常,您应该在错误发生时对其进行处理,至少在开始时是这样。您可以在可能引发错误的方法上使用 try/catch 块来执行此操作;或者,您可以通过创建抛出错误的方法来“冒出错误”以在适当的位置处理它,具体取决于您应用程序的体系结构。如果没有更多细节,我无法就错误处理的最佳方式提供准确的响应。在我的应用程序中,如果需要用户通知,我通常会举杯祝酒,如果出现网络故障等情况,我通常会举杯祝酒。

异常处理是一个复杂的问题,应该这样处理。对于如何处理错误,没有单一的正确答案。但是,一些基础知识是:

  1. 处理所有异常。如果您有一个抛出错误的方法,请确保包装在 try/catch 中并进行适当处理。这应该可以防止任何“未捕获的异常”。

  2. 具体说明您正在处理的错误。您似乎正在尝试为 activity 中的任何未捕获错误创建“catch-all”。我不会那样做。我会将所有代码包装在 try/catch 中,并在错误出现时进行处理。当我考虑你的代码时,我猜你得到一个空白屏幕的原因是你没有为你的错误创建布局 class,但我可能是错的。

TLDR;如果我是你,我会摆脱:

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

也去掉这个:

protected void onCreate(Bundle savedInstanceState) {
    Log.e(TAG, "onRestart: Hey just created");
    //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));

从那里我会找出哪些方法抛出错误并在您调用它们时处理它们。这将是最简单的方法。从那里您可以考虑更大更复杂的错误处理方法。您不一定需要为错误创建新的 activity。如果你愿意,我会在 finally 块中完成。确保您已经为错误 activity 创建了布局;否则,它将只是空白;将错误 [​​=50=] 添加到清单中(否则它会崩溃)。

try{
   //your add all your method calls here.
} catch (Exception E){
   //handle generic exception
} catch (ThreadException e){
   //handle the thread exception
   ...add more catches for more specific errors
} finally{
   //Optional call once all exceptions have been caught
   Intent error = new Intent(this, ErrorCatcher.class);
   startActivity(error);
}