exception 在android 中经常会遇到,那么遇到异常我们该如何解决,本文将举例解决部分android看法中遇到的异常。
一、nullpointerexception 空指针 nullpointerexception在开发中经常会碰到,比如引用的对象为空,数组为空等等都会引起空指针异常,如不及时处理,就会导致 应用crash。
1. 数组 nullpointerexception 不能向一个null数组元素赋值,获取长度,否则报
nullpointerexception: attempt to write to null array和
nullpointerexception attempt to get length of null array,以下代码会引起上面两种空指针异常。
2. 数组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; }
nullpointerexception 代码举例
3. 数组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 array12-27 17:17:44.627 8839 8839 e androidruntime: at com.programandroid.exception.nullpointerexception.arraynullpointer //产生空指针代码行 (nullpointerexception.java:32) 4. log 分析如下
数组nullpointerexception
空数组无法获取下标内容,如果获取则会导致nullpointerexception
12-27 17:23:24.168 11649 11649 e androidruntime: caused by: java.lang.nullpointerexception: attempt to write to null array12-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) 5. object 对象 nullpointerexception 对象空指针,这个是常见的空指针,主要是因为引用一个null 对象,进而导致空指针,常报以下错误
attempt to invoke a virtual method on a null object reference,以下代码可能会引起空指针异常。
6. object 对象 nullpointerexception 代码举例 简单代码举例如下:
public static void listnullpointer() { arraylist marraylist = null; marraylist.size(); }
object 对象 nullpointerexception
7. 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 reference12-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) 8. object 对象 nullpointerexception log 分析如下:
object nullpointerexception
9. nullpointerexception 解决方案 规避空指针举例如下:
1.使用时多注意判断对象是否为空
public static void listnullpointer() { arraylist marraylist = null; if (marraylist != null) { marraylist.size(); } }
使用对象是,最好判断对象是否为空
2.使用try-catch将抛出的异常抓住
try-catch 可以抓住抛出的异常,使应用程序不崩溃,但是,这个不是从根本上解决问题,会引起一些莫名其妙的问题。
public static void listnullpointer() { try { arraylist marraylist = null; marraylist.size(); } catch (exception e) { // todo: handle exception } }
try-catch 代码异常,防止app crash
二、 classcastexception 类型转换异常 classcastexception 类型转换异常:
此异常发生在类型转换时,并且在编译期间,编译器不会提示报错,但是当运行时,如果存在此异常,可能会导致app崩溃 crash。
比如当父类强制转换为子类时,classcastexception 就会发生
1. 以下代码 会引起 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() { } }
classcastexception 类型转换异常举例
2. classcastexception log 举例 classcastexception通常会打印以下类似信息
caused by: java.lang.classcastexception:com.programandroid.exception.exceptionactivity$fruit cannot be cast to com.programandroid.exception.exceptionactivity$apple 3. classcastexception log 分析
classcastexception log 分析
4. classcastexception 解决方案 使用try-catch抓住异常,或者从代码上解决根本问题。
使用 try-catch抓住 classcastexception异常
5. android 手机 settings classcastexception 解决方案 举例是为了更好的解决开发中的异常。比如在开发中,使用 monkey 测试settings模块时,报出的 classcastexception,settings代码比较多,一时也无法看完,此时,try-catch 也是一种不错的选择。
比如monkey测试某平台代码时,报出以下异常
log 信息如下:
fatal exception: applicationsstate.loader01-05 0356.101 6304 6941 e androidruntime: process: com.android.settings, pid: 630401-05 0356.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 0356.101 6304 6941 e androidruntime: at com.android.settings.applications.appstatenotificationbridge$3.filterapp(appstatenotificationbridge.java:110) 6. settings classcastexception log分析
settings classcastexception log1
settings classcastexception log2
7. setting crash classcastexception 解决方案:
try-catch 异常报错的地方
try-catch 异常报错的地方
try-catch 异常报错的地方
三、indexoutofboundsexception 下标越界异常 list 在开发中经常会被用的,那么错误的使用下标,将会导致indexoutofboundsexception 越界异常。以下代码就会引起indexoutofboundsexception异常
1. indexoutofboundsexception 代码举例
indexoutofboundsexception 异常举例
2. indexoutofboundsexception log举例 log 信息如下:
12-27 17:41:24.231 16891 16891 e androidruntime: caused by: java.lang.indexoutofboundsexception: index: 0, size: 012-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) 3. log 分析如下:
indexoutofboundsexception log分析
4. indexoutofboundsexception 解决方案 在使用时判断对象内容是否为0.
使用判断list 的size是否为0
四、activitynotfoundexception activitynotfoundexception 常见于eclipse 开发android中,android studio 已经帮忙自动生成activity,以及布局文件。
主要原因是未在androidmainfest.xml文件中注册,如未注册,会引起app crash ,crash log如下:
activitynotfoundexception: unable to find explicit activity class
1. activitynotfoundexception 代码举例 比如以下代码会引起此异常
activity未在androidmainfest.xml 中注册会引起activitynotfoundexception
2. 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) 3. log 分析如下:
activitynotfoundexception log分析
4. activitynotfoundexception 解决方案 在androidmainfest.xml中注册即可
四大组件一定,一定要在androidmainfest.xml 中注册
五、illegalstateexception illegalstateexception 非法状态异常,是因为软件中代码状态非法导致的。
以下代码会引起illegalstateexception 。当button控件声明android:onclick=illegalstateexception 却未在java代码中使用时,点击button,就会出现此类异常。
1. illegalstateexception 代码举例
illegalstateexception 代码举例
2. illegalstateexception log 举例 log信息如下:
12-27 16:07:41.158 1715 1715 e androidruntime: fatal exception: main12-27 16:07:41.158 1715 1715 e androidruntime: process: com.programandroid, pid: 171512-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) 3. illegalstateexception log分析如下:
illegalstateexception log截图
4. illegalstateexception 解决方案 illegalstateexception 类异常很多,不同的代码会有不同的解决方案,上述举例解决方案如下
illegalstateexception
六、 arrayindexoutofboundsexception 数组越界异常 数组在代码中经常被用到,当适用数组下标不当时,就会出现arrayindexoutofboundsexception。比如数组长度为4,但你要引用下标为5的元素,这时候,就会异常crash。
1. arrayindexoutofboundsexception 代码举例: public static void arrayindexoutofbounds() { string[] mstrings = { a, b, c, d }; string testsstring = mstrings[5]; }
arrayindexoutofboundsexception 代码举例
2. arrayindexoutofboundsexception log举例: log信息如下:
12-27 17:51:15.420 19185 19185 e androidruntime: caused by: java.lang.arrayindexoutofboundsexception: length=4; index=512-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 3. arrayindexoutofboundsexception log分析如下:
arrayindexoutofboundsexception log分析
4. arrayindexoutofboundsexception解决方案 1.正确使用数组下标
2.如果不确定数组长度,请先获取长度,然后在判断下标是否大于等于数组长度。
3.try-catch 抓住异常,防止crash,但不能从根本上解决问题。
七、securityexception 安全异常 securityexception 安全异常在android 中也会经常发生,主要是android 的安全机制原因造成的,为了管理应用获取手机的一些敏感信息,android安全机制规定,必须在androidmainfest.xml文件中声明,并且,android 6.0之后,获取手机敏感信息时候,需要动态申请权限,只有用户授权后才可以获取手机敏感信息。
1. securityexception 代码举例 获取手机的imei 号属于手机的敏感信息
/** * * * * * */ 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; } }
获取手机imei号
2. securityexception log举例 12-27 1855.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 1855.663 21467 21467 e androidruntime: at android.os.parcel.readexception(parcel.java:1683)12-27 1855.663 21467 21467 e androidruntime: at android.os.parcel.readexception(parcel.java:1636)12-27 1855.663 21467 21467 e androidruntime: at com.android.internal.telephony.itelephony$stub$proxy.getdeviceid(itelephony.java:4281) 3. securityexception log 分析
securityexception log 分析
4. securityexception 解决方案 android 6.0之前,在androidmainfest.xml中申请权限即可,
android 6.0 之后,请动态申请权限。
androidmainfest.xml 中申请权限
八、illegalargumentexception: service not registered 服务未注册异常 1.报错信息如下: 01-30 09:10:26.257 23681 23681 w system.err: java.lang.illegalargumentexception: service not registered: com.programandroid.exception.exceptionactivity$1@5f3161e01-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) 2.log分析如下:
log 分析
此异常经常发生在错误的解除绑定服务造成的,解决方法:
1.解除绑定服务之前,先判断是否绑定过,只有绑定过后才可以解绑
2.使用try-catch 抓取住异常
代码举例如下:
service not registered 异常举例
九、badtokenexception 解决方案 1. log 举例 03-12 14:55:13.734 5564 5564 e androidruntime: fatal exception: main03-12 14:55:13.734 5564 5564 e androidruntime: process: com.android.fmradio, pid: 556403-12 14:55:13.734 5564 5564 e androidruntime: java.lang.runtimeexception: error receiving broadcast intent { act=android.intent.action.headset_plug flg=0x40000010 (has extras) } in com.android.fmradio.fmservice$fmservicebroadcastreceiver@b3d2a0303-12 14:55:13.734 5564 5564 e androidruntime: at android.app.loadedapk$receiverdispatcher$args.lambda$getrunnable$0(loadedapk.java:1401)03-12 14:55:13.734 5564 5564 e androidruntime: at android.app.-$$lambda$loadedapk$receiverdispatcher$args$_bumdx2uksnxlvre6ujsjzkotua.run(unknown source:2)03-12 14:55:13.734 5564 5564 e androidruntime: at android.os.handler.handlecallback(handler.java:873)03-12 14:55:13.734 5564 5564 e androidruntime: at android.os.handler.dispatchmessage(handler.java:99)03-12 14:55:13.734 5564 5564 e androidruntime: at android.os.looper.loop(looper.java:193)03-12 14:55:13.734 5564 5564 e androidruntime: at android.app.activitythread.main(activitythread.java:6702)03-12 14:55:13.734 5564 5564 e androidruntime: at java.lang.reflect.method.invoke(native method)03-12 14:55:13.734 5564 5564 e androidruntime: at com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:493)03-12 14:55:13.734 5564 5564 e androidruntime: at com.android.internal.os.zygoteinit.main(zygoteinit.java:911)03-12 14:55:13.734 5564 5564 e androidruntime: caused by: android.view.windowmanager$badtokenexception: unable to add window android.view.viewrootimpl$w@f652dba -- permission denied for window type 200303-12 14:55:13.734 5564 5564 e androidruntime: at android.view.viewrootimpl.setview(viewrootimpl.java:851)03-12 14:55:13.734 5564 5564 e androidruntime: at android.view.windowmanagerglobal.addview(windowmanagerglobal.java:356)03-12 14:55:13.734 5564 5564 e androidruntime: at android.view.windowmanagerimpl.addview(windowmanagerimpl.java:93)03-12 14:55:13.734 5564 5564 e androidruntime: at android.app.dialog.show(dialog.java:329)03-12 14:55:13.734 5564 5564 e androidruntime: at com.android.fmradio.fmservice$fmservicebroadcastreceiver.onreceive(fmservice.java:322)03-12 14:55:13.734 5564 5564 e androidruntime: at android.app.loadedapk$receiverdispatcher$args.lambda$getrunnable$0(loadedapk.java:1391)03-12 14:55:13.734 5564 5564 e androidruntime: ... 8 more 2.产生原因 android 8.0 之后如果要弹出系统弹窗,需要使用 type_application_overlay以及
来进行系统弹窗,否则会报以下异常badtokenexception: unable to add window android.view.viewrootimpl$w@f652dba -- permission denied for window type 2003
3. 解决方案 系统弹窗,请用type_application_overlay 替换之前的windows type。
dialog mfmdialog = new alertdialog.builder(context) .settitle(r.string.airplane_title).setmessage(r.string.airplane_message) .setpositivebutton(r.string.close_fm, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } } ).setcancelable(false).create(); // android 8.0 之后弹出系统弹窗,需要使用 type_application_overlay // // 一下两个 之前常用的系统的dialog 会报 // badtokenexception: unable to add window android.view.viewrootimpl$w@f652dba -- permission denied for window type 2003 //mfmdialog.getwindow().settype(windowmanager.layoutparams.type_system_dialog); //mfmdialog.getwindow().settype(windowmanager.layoutparams.type_system_alert); mfmdialog.getwindow().settype(windowmanager.layoutparams.type_application_overlay); mfmdialog.show(); 4. 参考 google android go 行为变更 google 官方链接如下:
android 8.0 alert 弹窗行为变更
android 8.0 alert 弹窗行为变更
荣耀畅玩6X、红米note4X和360N5对比评测,换你选哪个?
飞兆半导体推出高速MOSFET/IGBT栅极驱动光耦合器FO
人工智能的发展对内容营销行业有什么影响
电厂配电室中六氟化硫气体泄漏报警传感器的重要应用
圣诞节送女朋友什么礼物好?佩戴舒适高颜值蓝牙耳机推荐
BadTokenException解决方案
荣耀V9对比vivo X9 Plus手机评测:内外兼修颇具优势
基于去中心化的DHT的优劣势分析
iPhone8什么时候上市:iPhone8黑科技太多要延期发布,华为Mate10乘虚而入狙击iPhone8!
超声波电机是什么,它的特点是什么
微软、动视暴雪同意推迟并购交易截止期限
华为路由器实现单臂路由的编程分享
华为Mate9涉嫌闪存虚假宣传:77人联名起诉
天津联通携手华为完成跨大区5G 2B数据业务,已具备商用基础条件
DS2711,DS2712 标准NiMH电池充电器
浅谈SylixOS 实时操作系统中Go语言应用
桁架机器人分为四类
德国车到电网试点项目提升可再生能源的利用率
iPronics推出一款款完全可编程光子微芯片
ORCA-FusionBT相机装机配件清单