8 种 Android App Crash 解决套路

640?wx_fmt=gif

640?wx_fmt=gif

极力推荐文章:欢迎收藏

Android 干货分享 640?wx_fmt=gif

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

Exception 在Android 中经常会遇到,那么遇到异常我们该如何解决,本文将举例解决部分Android看法中遇到的异常。

通过本篇文章,您将收获以下内容

  1. NullPointerException 空指针

  2. ClassCastException 类型转换异常

  3. IndexOutOfBoundsException 下标越界异常

  4. ActivityNotFoundException Activity未找到异常

  5. IllegalStateException 非法状态异常

  6. ArrayIndexOutOfBoundsException 数组越界异常

  7. SecurityException 安全异常

  8. IllegalArgumentException: Service not registered 服务未注册异常

1. NullPointerException 空指针

NullPointerException在开发中经常会碰到,比如引用的对象为空,数组为空等等都会引起空指针异常,如不及时处理,就会导致 应用Crash

1. 数组 NullPointerException

不能向一个null数组元素赋值,获取长度,否则报NullPointerException: Attempt to write to null arrayNullPointerException Attempt to get length of null array,以下代码会引起上面两种空指针异常。

数组NullPointerException 代码举例

    public static void ArrayNullPointer() {
        /**
         * 数组空指针 NullPointerException
         *
         * 1.获取null数组长度
         * 2.为null 数组元素复制
         * */
        int[] array = null;
        // 1. NullPointerException: Attempt to get length of null array
        int length = array.length;
        // 2. NullPointerException: Attempt to write to null array
        array[0] = 1;

    }

640?wx_fmt=other

NullPointerException 代码举例

数组NullPointerException Log 举例

  • Log 信息如下

获取 空数组长度导致的 NullPointerException 如下:

12-27 17:17:44.627  8839  8839 E AndroidRuntime:  Caused by: java.lang.NullPointerException:
                                                   Attempt to get length of null array
12-27 17:17:44.627  8839  8839 E AndroidRuntime:    at com.programandroid.Exception.NullPointerException.ArrayNullPointer
                                                   //产生空指针代码行
                                                   (NullPointerException.java:32)

Log 分析如下

640?wx_fmt=other

数组NullPointerException

空数组无法获取下标内容,如果获取则会导致NullPointerException

12-27 17:23:24.168 11649 11649 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to write to null array
12-27 17:23:24.168 11649 11649 E AndroidRuntime:    at com.programandroid.Exception.NullPointerException.ArrayNullPointer(NullPointerException.java:34)
12-27 17:23:24.168 11649 11649 E AndroidRuntime:    at com.programandroid.Exception.ExceptionActivity.NullPointerException(ExceptionActivity.java:37)

Log 分析如下

640?wx_fmt=other

null 数组元素赋值异常

  1. Object 对象 NullPointerException

对象空指针,这个是常见的空指针,主要是因为引用一个null 对象,进而导致空指针,常报以下错误Attempt to invoke a virtual method on a null object reference,以下代码可能会引起空指针异常。

object 对象 NullPointerException 代码举例

简单代码举例如下:

    public static void ListNullPointer() {

        ArrayList<String> mArrayList = null;
            mArrayList.size();
    }

640?wx_fmt=other

Object 对象 NullPointerException

object 对象 NullPointerException log 举例

  • Log 信息如下:

12-27 17:28:22.565 12725 12725 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
12-27 17:28:22.565 12725 12725 E AndroidRuntime:    at com.programandroid.Exception.NullPointerException.ListNullPointer(NullPointerException.java:45)
12-27 17:28:22.565 12725 12725 E AndroidRuntime:    at com.programandroid.Exception.ExceptionActivity.NullPointerException(ExceptionActivity.java:37)

object 对象 NullPointerException Log 分析如下:

640?wx_fmt=other

Object NullPointerException

NullPointerException 解决方案

  1. 使用时多注意判断对象是否为空

    public static void ListNullPointer() {

        ArrayList<String> mArrayList = null;
        if (mArrayList != null) {
            mArrayList.size();
        }
    }

640?wx_fmt=other

使用对象是,最好判断对象是否为空

  1. 使用try-catch将抛出的异常抓住

try-catch 可以抓住抛出的异常,使应用程序不崩溃,但是,这个不是从根本上解决问题,会引起一些莫名其妙的问题。

    public static void ListNullPointer() {
            try {
                ArrayList<String> mArrayList = null;
                mArrayList.size();
            } catch (Exception e) {
                // TODO: handle exception
            }
    }

640?wx_fmt=other

try-catch 代码异常,防止app crash

  1. 终极解决方案,优化代码逻辑,从根本上解决问题。

2. ClassCastException 类型转换异常

ClassCastException 类型转换异常:app崩溃 crash父类强制转换为子类时,ClassCastException 就会发生

1. 以下代码 会引起 ClassCastException

ClassCastException 代码举例

请勿 父类强制转换为子类,否则就会发生ClassCastException异常。

public void ClassCastExample() {
        Fruit banana = new Fruit();
        /**
         * ClassCastException
         *
         * 1. 此处强制转换,会导致 app 编译没问题,运行挂掉, Caused by:
         * java.lang.ClassCastException:
         * com.programandroid.Exception.ExceptionActivity$ Fruit cannot be cast
         * to com.programandroid.Exception.ExceptionActivity$Apple
         *
         ***/
        Apple apple = (Apple) banana;

    }

    /**
     * ClassCastException
     *
     * 2. 此处强转回导致app crash return (Apple) banana;
     * */
    public Apple isRight() {
        Fruit banana = new Fruit();
        return (Apple) banana;
    }

    class Fruit {
        public Fruit() {
        }
    }

    class Apple extends Fruit {
        public Apple() {
        }
    }

640?wx_fmt=other

ClassCastException 类型转换异常举例

ClassCastException Log 举例

ClassCastException通常会打印以下类似信息

Caused by: java.lang.ClassCastException:
com.programandroid.Exception.ExceptionActivity$
Fruit cannot be cast to com.programandroid.Exception.ExceptionActivity$Apple

ClassCastException Log 分析

640?wx_fmt=other

ClassCastException log 分析

ClassCastException 解决方案

使用try-catch抓住异常,或者从代码上解决根本问题。

640?wx_fmt=other

使用 try-catch抓住 ClassCastException异常

2. Android 手机 Settings ClassCastException 解决方案

举例是为了更好的解决开发中的异常。比如在开发中,使用 monkey 测试Settings模块时,报出的 ClassCastExceptionSettings代码比较多,一时也无法看完,此时,try-catch 也是一种不错的选择。monkey测试某平台代码时,报出以下异常

Settings ClassCastException Log 举例

  • log 信息如下:

FATAL EXCEPTION: ApplicationsState.Loader
01-05 03:36:56.101  6304  6941 E AndroidRuntime: Process: com.android.settings, PID: 6304
01-05 03:36:56.101  6304  6941 E AndroidRuntime: java.lang.ClassCastException:
                                                   com.android.settings.datausage.AppStateDataUsageBridge$DataUsageState 
                                                   cannot be cast to com.android.settings.notification.NotificationBackend$AppRow
                                                   
01-05 03:36:56.101  6304  6941 E AndroidRuntime:    at com.android.settings.applications.AppStateNotificationBridge$3.filterApp(AppStateNotificationBridge.java:110)

Settings ClassCastException Log分析

640?wx_fmt=other

Settings ClassCastException Log1

640?wx_fmt=other

Settings ClassCastException Log2

Setting crash ClassCastException 解决方案:

640?wx_fmt=other

try-catch 异常报错的地方

640?wx_fmt=other

try-catch 异常报错的地方

640?wx_fmt=other

try-catch 异常报错的地方

3. IndexOutOfBoundsException 下标越界异常

List 在开发中经常会被用的,那么错误的使用下标,将会导致IndexOutOfBoundsException越界异常。以下代码就会引起IndexOutOfBoundsException异常

IndexOutOfBoundsException 代码举例

640?wx_fmt=other

IndexOutOfBoundsException 异常举例

IndexOutOfBoundsException Log举例

  • Log 信息如下:

12-27 17:41:24.231 16891 16891 E AndroidRuntime: Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
12-27 17:41:24.231 16891 16891 E AndroidRuntime:    at java.util.ArrayList.get(ArrayList.java:411)
12-27 17:41:24.231 16891 16891 E AndroidRuntime:    at com.programandroid.Exception.IndexOutOfBoundsException.isAppOnRecent(IndexOutOfBoundsException.java:40)
12-27 17:41:24.231 16891 16891 E AndroidRuntime:    at com.programandroid.Exception.ExceptionActivity.IndexOutOfBoundsException(ExceptionActivity.java:80)

Log 分析如下:

640?wx_fmt=other

IndexOutOfBoundsException Log分析

IndexOutOfBoundsException 解决方案

在使用时判断对象内容是否为0.

640?wx_fmt=other

使用判断List 的size是否为0

4. ActivityNotFoundException

ActivityNotFoundException 常见于Eclipse 开发Android中,Android studio 已经帮忙自动生成Activity,以及布局文件。AndroidMainfest.xml文件中注册,如未注册,会引起app crash ,crash log如下:ActivityNotFoundException: Unable to find explicit activity class

ActivityNotFoundException 代码举例

比如以下代码会引起此异常

640?wx_fmt=other

Activity未在Androidmainfest.xml 中注册会引起ActivityNotFoundException

ActivityNotFoundException Log 举例

  • Log信息如下:

12-27 17:46:05.994 17893 17893 E AndroidRuntime: Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.programandroid/com.programandroid.Test.TestActivity}; have you declared this activity in your AndroidManifest.xml?
12-27 17:46:05.994 17893 17893 E AndroidRuntime:    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1810)

Log 分析如下:

640?wx_fmt=other

ActivityNotFoundException Log分析

ActivityNotFoundException 解决方案

AndroidMainfest.xml中注册即可

640?wx_fmt=other

四大组件一定,一定要在AndroidMainfest.xml 中注册

5. IllegalStateException

IllegalStateException 非法状态异常,是因为软件中代码状态非法导致的。IllegalStateException 。当Button控件声明android:onClick="IllegalStateException" 却未在Java代码中使用时,点击Button,就会出现此类异常。

IllegalStateException 代码举例

640?wx_fmt=other

IllegalStateException 代码举例

IllegalStateException Log 举例

  • log信息如下:

12-27 16:07:41.158  1715  1715 E AndroidRuntime: FATAL EXCEPTION: main
12-27 16:07:41.158  1715  1715 E AndroidRuntime: Process: com.programandroid, PID: 1715
12-27 16:07:41.158  1715  1715 E AndroidRuntime: java.lang.IllegalStateException: 
                                                Could not find method IllegalStateException(View) in a parent
                                                or ancestor Context for android:onClick attribute defined on view class 
                                                android.widget.Button with id 'btn_on_click'
12-27 16:07:41.158  1715  1715 E AndroidRuntime:    at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4781)
12-27 16:07:41.158  1715  1715 E AndroidRuntime:    at android.view.View$DeclaredOnClickListener.onClick(View.java:4740)

IllegalStateException Log分析如下:

640?wx_fmt=other

IllegalStateException Log截图

IllegalStateException 解决方案

IllegalStateException 类异常很多,不同的代码会有不同的解决方案,上述举例解决方案如下

640?wx_fmt=other

IllegalStateException

6 . ArrayIndexOutOfBoundsException 数组越界异常

数组在代码中经常被用到,当适用数组下标不当时,就会出现ArrayIndexOutOfBoundsException。比如数组长度为4,但你要引用下标为5的元素,这时候,就会异常crash

ArrayIndexOutOfBoundsException 代码举例:

    public static void ArrayIndexOutOfBounds() {

        String[] mStrings = { "a", "b", "c", "d" };
        String testsString = mStrings[5];
    }

640?wx_fmt=other

ArrayIndexOutOfBoundsException 代码举例

ArrayIndexOutOfBoundsException Log举例:

  • Log信息如下:

12-27 17:51:15.420 19185 19185 E AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=5
12-27 17:51:15.420 19185 19185 E AndroidRuntime:    at com.programandroid.Exception.ArrayIndexOutOfBoundsException.ArrayIndexOutOfBounds(ArrayIndexOutOfBoundsException.java:20)
12-27 17:51:15.420 19185 19185 E AndroidRuntime:    at com.programandroid.Exception.ExceptionActivity.ArrayIndexOutOfBoundsException(ExceptionActivity.java:105)
12-27 17:51:15.420 19185 19185 E AndroidRuntime:    ... 11 more

ArrayIndexOutOfBoundsException Log分析如下:

640?wx_fmt=other

ArrayIndexOutOfBoundsException Log分析

ArrayIndexOutOfBoundsException解决方案

  1. 正确使用数组下标

  1. 如果不确定数组长度,请先获取长度,然后在判断下标是否大于等于数组长度。

  1. try-catch 抓住异常,防止crash,但不能从根本上解决问题。

7. SecurityException 安全异常

SecurityException 安全异常在Android 中也会经常发生,主要是Android 的安全机制原因造成的,为了管理应用获取手机的一些敏感信息,Android安全机制规定,必须在AndroidMainfest.xml 文件中声明,并且,Android 6.0之后,获取手机敏感信息时候,需要动态申请权限,只有用户授权后才可以获取手机敏感信息。

SecurityException 代码举例

获取手机的IMEI 号属于手机的敏感信息

/**
     *
     * <!-- 读取手机IMEI的设备权限 -->
     *
     * <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     * */
    public static String getIMEI(Context context) {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        if (deviceId == null) {
            return "UnKnown";
        } else {
            return deviceId;
        }
    }

640?wx_fmt=other

获取手机IMEI号

SecurityException log举例

12-27 18:05:55.663 21467 21467 E AndroidRuntime: Caused by: java.lang.SecurityException: getDeviceId: Neither user 10117 nor current process has android.permission.READ_PHONE_STATE.
12-27 18:05:55.663 21467 21467 E AndroidRuntime:    at android.os.Parcel.readException(Parcel.java:1683)
12-27 18:05:55.663 21467 21467 E AndroidRuntime:    at android.os.Parcel.readException(Parcel.java:1636)
12-27 18:05:55.663 21467 21467 E AndroidRuntime:    at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4281)

SecurityException log 分析

640?wx_fmt=other

SecurityException log 分析

SecurityException 解决方案

Android 6.0之前,在AndroidMainfest.xml中申请权限即可,Android 6.0 之后,请动态申请权限。

640?wx_fmt=other

AndroidMainfest.xml 中申请权限

8. IllegalArgumentException: Service not registered 服务未注册异常

报错信息如下:

01-30 09:10:26.257 23681 23681 W System.err: java.lang.IllegalArgumentException: Service not registered: com.programandroid.Exception.ExceptionActivity$1@5f3161e
01-30 09:10:26.257 23681 23681 W System.err:    at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1363)
01-30 09:10:26.257 23681 23681 W System.err:    at android.app.ContextImpl.unbindService(ContextImpl.java:1499)
01-30 09:10:26.257 23681 23681 W System.err:    at android.content.ContextWrapper.unbindService(ContextWrapper.java:648)
01-30 09:10:26.257 23681 23681 W System.err:    at com.programandroid.Exception.ExceptionActivity.ServiceNotRegisteredCrash(ExceptionActivity.java:276)
01-30 09:10:26.257 23681 23681 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
01-30 09:10:26.258 23681 23681 W System.err:    at android.view.View$DeclaredOnClickListener.onClick(View.java:4744)
01-30 09:10:26.258 23681 23681 W System.err:    at android.view.View.performClick(View.java:5675)

Log分析如下:

640?wx_fmt=other

Log 分析

此异常经常发生在错误的解除绑定服务造成的,解决方法:try-catch 抓取住异常

640?wx_fmt=other

Service not registered 异常举例

640?wx_fmt=png

坚持就有惊喜

posted @ 2019-08-09 10:39  程序员Android的博客  阅读(837)  评论(0编辑  收藏  举报