简体   繁体   English

防止应用崩溃的全局方法?

[英]Global method to prevent app from crashing?

I am trying to add a method in my parent activity that all my activities are inheriting from. 我试图在我的所有活动都继承自的父活动中添加一个方法。 I want the method to catch any errors that have not already been handled so the app does not crash. 我希望该方法捕获尚未处理的所有错误,以使应用程序不会崩溃。 Instead of crashing it will redirect to a failure screen activity. 它不会崩溃,而是会重定向到故障屏幕活动。

Here is what I have at the moment but it does not work, the app freezes and then goes black: 这是我目前所拥有的,但是它不起作用,该应用程序冻结,然后变黑:

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

The uncaught exception handler is not meant for rescuing an application. 未捕获的异常处理程序并不旨在拯救应用程序。 Ending up in that handler means the thread is being terminated. 在该处理程序中结束表示该线程正在终止。 The handler gets notified as a courtesy for logging purposes before the thread is killed. 在线程被杀死之前,出于日志记录的目的,该处理程序得到了礼貌的通知。

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. 如果未设置显式处理程序,则线程的组是默认处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM