gpt4 book ai didi

java - 防止应用程序崩溃的全局方法?

转载 作者:行者123 更新时间:2023-12-01 12:49:01 25 4
gpt4 key购买 nike

我正在尝试在我的父 Activity 中添加一个方法,我的所有 Activity 都继承自该方法。我希望该方法能够捕获尚未处理的任何错误,以便应用程序不会崩溃。它不会崩溃,而是重定向到故障屏幕 Activity 。

这是我目前所拥有的,但它不起作用,应用程序卡住然后变黑:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
redirectToFailureScreen();
}
});

最佳答案

未捕获的异常处理程序并不用于拯救应用程序。在该处理程序中结束意味着线程正在被终止。在线程被终止之前,出于记录目的,处理程序会收到通知。

Implemented by objects that want to handle cases where a thread is being terminated by an uncaught exception. Upon such termination, the handler is notified of the terminating thread and causal exception. If there is no explicit handler set then the thread's group is the default handler.

关于java - 防止应用程序崩溃的全局方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24399571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com