![](/img/trans.png)
[英]Android Context.startActivity(Intent) throws runtime exception
[英]Why context.startActivity(intent) not starting the activity and how to handle exception in android?
我有一个异常处理程序来处理来自活动 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);
}
}
它从活动 class 初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "onRestart: Hey just created");
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));
// other methods and function
}
当控件进入异常处理程序时,不会创建活动,而是挂起我的应用程序的空白页面。
我得到的唯一消息是
I/Timeline: Timeline: Activity_launch_request time:417281208 intent:Intent { cmp=com.yyy.xxx/com.yyy.xxx.Activities.Error }
编辑:好的,经过一番挖掘,我发现,如果没有抛出异常,则启动活动(即我可以看到活动页面),但是当抛出异常时,就会显示空白页面。 为什么只有在抛出异常时才会出现这种情况,以及在 android 中处理异常的更好方法是什么? 有什么帮助吗?
编辑 2 :它类似于这个https://codecrunch.co/blog/custom-error-handling-in-android/
在 startActivity 之前,添加error.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
. 这里也有更完整的答案
当我使用 try/catch 而不是
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this.getApplicationContext()));
要处理异常,它可能不是最好的方法,但值得一试。
此代码可能对您有帮助
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
来尝试启动活动。 这可能会导致您的一些问题。 这是可以做到的,但我认为有一种更简单的方法可以让这一切发挥作用。
还要注意的是你忘记了代码里面的rest
@Override
public void uncaughtException
....
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
为什么只有在抛出异常时才会出现这种情况......
这是因为您已经覆盖了uncaughtException
方法。 因此,当您遇到未捕获的异常时,您将启动Error.class
活动。
@Override
public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
Intent error = new Intent(activity, ErrorCatcher.class);
activity.startActivity(error);
}
...以及在 android 中处理异常的更好方法是什么?
有多种方法可以处理异常。 这实际上取决于用例是什么。 一般来说,您应该在错误发生时处理它们,至少在开始时是这样。 您可以在可能引发错误的方法上使用try/catch
块来执行此操作; 或者,您可以通过创建引发错误的方法来“冒泡错误”以在适当的位置处理它,具体取决于您的应用程序的架构。 如果没有更多细节,我无法就处理错误的最佳方式提供准确的响应。 在我的应用程序中,如果需要用户通知,我通常会举杯祝酒,如果出现网络故障等问题,我通常会举杯。
异常处理是一个复杂的问题,应该这样处理。 对于如何处理错误,没有单一的正确答案。 但是,一些基础知识是:
处理所有异常。 如果您有一个引发错误的方法,请确保您包含在try/catch
中并正确处理。 这应该防止任何“未捕获的异常”。
具体说明您正在处理的错误。 您似乎正在尝试为活动中的任何未捕获错误创建“包罗万象”。 我不会 go 这样说。 我会将所有代码包装在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()));
从那里我会弄清楚哪些方法会引发错误并在您调用它们时处理它们。 这将是最简单的方法。 从那里您可以考虑一种更大、更复杂的错误处理方法。 您不一定需要为错误创建新活动。 如果你愿意,我会在 finally 块中进行。 确保您已经为错误活动创建了布局; 否则,它将是空白的; 将错误活动添加到清单中(否则它将崩溃)。
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);
}
在您覆盖的 uncaughtException 方法上,您最好使用此代码启动活动
startActivity(activity,Error.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.