有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我希望全局捕获所有可能的安卓异常,以便应用程序无法关闭

我想在一个类中捕获所有可用的异常原因,这样应用程序就不会意外关闭。 我为它写了一些代码,但它不起作用

我的申请。java类




import 安卓.app.Application;

public class MyApplication extends Application {

    private static MyApplication mInstance;
    private Thread.UncaughtExceptionHandler defaultUEH;
    private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
            new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread thread, Throwable ex) {

                    // here I do logging of exception to a db
//                    Log.d("MyApp", "Uncaught exception.");
                    // Do what you want.

                    // re-throw exception to O.S. if that is serious and need to be handled by o.s. Uncomment the next line at that time.
                    //defaultUEH.uncaughtException(thread, ex);
                }
            };

    public MyApplication() {
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }

 


}

那么我如何调用_unCaughtExceptionHandler对象呢


共 (0) 个答案