Android 如何避免运行时奔溃

奔溃问题

android运行的时候难免会有一些空指针(NullPointerException)或者下标越界(IndexOutOfBoundsException),用户使用的过程操作某一个按钮的时候,就发生了崩溃.导致app直接闪退,降低用户体验,因此导致用户流失

 

1.在项目根目录的build.gradle文件中添加

1
2
3
4
5
allprojects {
    repositories {
        maven { url 'https://dl.bintray.com/xuuhaoo/maven/'}
    }
}

2. 在项目的build.gradle文件中添加依赖包

1
implementation 'com.tonystark.android:defense_crash:2.0.0'

3.在项目的Application中初始化操作

1
2
3
DefenseCrash.initialize();
      
DefenseCrash.install(this);

 4.在项目的Application中实现 implements IExceptionHandler 接口

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MyApp extends Application implements IExceptionHandler {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // step1: Initialize the lib.
        DefenseCrash.initialize();
        // setp2: Install the fire wall defense.
        DefenseCrash.install(this);
    }
 
    @Override
    public void onCaughtException(Thread thread, Throwable throwable, boolean isSafeMode) {
        // step3: Print the error when crashed during runtime.
        throwable.printStackTrace();
        // step4: Upload this throwable to your crash collection sdk.
    }
 
    @Override
    public void onMayBeBlackScreen(Throwable throwable) {
        // onLayout(),onMeasure() or onDraw() has breaks down,
        // it causes the drawing to be abnormal and the choreographer to break down.
        // We will notify you on this method,you’d better finish this activity or restart the application.
    }
 
    @Override
    public void onEnterSafeMode() {
        // We enter the safe mode to keep the main looper loop after crashed.You’d better do nothing here,we just notify you.
    }
 
}

 

 5.大功告成,这样用户在使用app是就不会出现奔溃闪退的现象啦!!!

 

posted @   monkey0928  阅读(746)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示