在 android 上使用全局异常处理 [英] Using Global Exception Handling on android

查看:29
本文介绍了在 android 上使用全局异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有代码示例或关于如何使用 Thread.setDefaultUncaughtExceptionHandler 方法的教程?基本上,我正在尝试在我的应用程序中显示自定义警报对话框,无论何时抛出异常.是否有可能做到这一点?我知道如果在 UI 线程中抛出异常,在屏幕上显示一些东西有点棘手,但我不知道有什么解决办法.

Is there a code example, or a tutorial on how to use the Thread.setDefaultUncaughtExceptionHandler method? Basically I'm trying to display a custom alert dialog, whenever an exception is thrown, in my application. Is it possible to do this? I know it's a little bit tricky to display something on the screen, if the exception is thrown in the UI thread but I don't know any work around for this.

推荐答案

访问此页面并提出解决方案的人的基本示例 :)

Basic Example for someone who comes to this page with a solution :)

public class ChildActivity extends BaseActivity {
    @SuppressWarnings("unused")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int a=1/0;
    }
}

处理错误的类:

public class BaseActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                Log.e("Alert","Lets See if it Works !!!");
            }
        });
    }
}

这篇关于在 android 上使用全局异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆