Exception
在Android
中經(jīng)常會(huì)遇到,那么遇到異常我們?cè)撊绾谓鉀Q,本文將舉例解決部分Android
看法中遇到的異常。
一、NullPointerException 空指針
NullPointerException
在開發(fā)中經(jīng)常會(huì)碰到,比如引用的對(duì)象為空,數(shù)組為空等等都會(huì)引起空指針異常,如不及時(shí)處理,就會(huì)導(dǎo)致 應(yīng)用Crash
。
1. 數(shù)組 NullPointerException
不能向一個(gè)null
數(shù)組元素賦值,獲取長(zhǎng)度,否則報(bào)NullPointerException: Attempt to write to null array
和NullPointerException Attempt to get length of null array
,以下代碼會(huì)引起上面兩種空指針異常。
2. 數(shù)組NullPointerException 代碼舉例
public static void ArrayNullPointer() {
/**
* 數(shù)組空指針 NullPointerException
*
* 1.獲取null數(shù)組長(zhǎng)度
* 2.為null 數(shù)組元素復(fù)制
* */
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. 數(shù)組NullPointerException Log 舉例
-
Log 信息如下
獲取 空數(shù)組長(zhǎng)度導(dǎo)致的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
//產(chǎn)生空指針代碼行
(NullPointerException.java:32)
4. Log 分析如下
數(shù)組NullPointerException
空數(shù)組無法獲取下標(biāo)內(nèi)容,如果獲取則會(huì)導(dǎo)致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)
5.Object
對(duì)象NullPointerException
對(duì)象空指針,這個(gè)是常見的空指針,主要是因?yàn)橐靡粋€(gè)null
對(duì)象,進(jìn)而導(dǎo)致空指針,常報(bào)以下錯(cuò)誤Attempt to invoke a virtual method on a null object reference
,以下代碼可能會(huì)引起空指針異常。
6. object 對(duì)象 NullPointerException 代碼舉例
簡(jiǎn)單代碼舉例如下:
public static void ListNullPointer() {
ArrayList<String> mArrayList = null;
mArrayList.size();
}
Object 對(duì)象 NullPointerException
7. object 對(duì)象 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)
8. object 對(duì)象 NullPointerException Log 分析如下:
Object NullPointerException
9. NullPointerException 解決方案
規(guī)避空指針舉例如下:
-
1.使用時(shí)多注意判斷對(duì)象是否為空
public static void ListNullPointer() {
ArrayList<String> mArrayList = null;
if (mArrayList != null) {
mArrayList.size();
}
}
使用對(duì)象是,最好判斷對(duì)象是否為空
-
2.使用
try-catch
將拋出的異常抓住
try-catch
可以抓住拋出的異常,使應(yīng)用程序不崩潰,但是,這個(gè)不是從根本上解決問題,會(huì)引起一些莫名其妙的問題。
public static void ListNullPointer() {
try {
ArrayList<String> mArrayList = null;
mArrayList.size();
} catch (Exception e) {
// TODO: handle exception
}
}
try-catch 代碼異常,防止app crash
二、 ClassCastException 類型轉(zhuǎn)換異常
ClassCastException
類型轉(zhuǎn)換異常:
此異常發(fā)生在類型轉(zhuǎn)換時(shí),并且在編譯期間,編譯器不會(huì)提示報(bào)錯(cuò),但是當(dāng)運(yùn)行時(shí),如果存在此異常,可能會(huì)導(dǎo)致app
崩潰crash
。
比如當(dāng)父類
強(qiáng)制轉(zhuǎn)換為子類
時(shí),ClassCastException 就會(huì)發(fā)生
1. 以下代碼 會(huì)引起 ClassCastException
請(qǐng)勿 父類強(qiáng)制轉(zhuǎn)換為子類,否則就會(huì)發(fā)生ClassCastException
異常。
public void ClassCastExample() {
Fruit banana = new Fruit();
/**
* ClassCastException
*
* 1. 此處強(qiáng)制轉(zhuǎn)換,會(huì)導(dǎo)致 app 編譯沒問題,運(yùn)行掛掉, 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. 此處強(qiáng)轉(zhuǎn)回導(dǎo)致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 類型轉(zhuǎn)換異常舉例
2. ClassCastException Log 舉例
ClassCastException
通常會(huì)打印以下類似信息
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 手機(jī) Settings ClassCastException 解決方案
舉例是為了更好的解決開發(fā)中的異常。比如在開發(fā)中,使用monkey
測(cè)試Settings
模塊時(shí),報(bào)出的ClassCastException
,Settings
代碼比較多,一時(shí)也無法看完,此時(shí),try-catch
也是一種不錯(cuò)的選擇。
比如monkey
測(cè)試某平臺(tái)代碼時(shí),報(bào)出以下異常
-
log 信息如下:
FATAL EXCEPTION: ApplicationsState.Loader
01-05 0356.101 6304 6941 E AndroidRuntime: Process: com.android.settings, PID: 6304
01-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 異常報(bào)錯(cuò)的地方
try-catch 異常報(bào)錯(cuò)的地方
try-catch 異常報(bào)錯(cuò)的地方
三、IndexOutOfBoundsException 下標(biāo)越界異常
List 在開發(fā)中經(jīng)常會(huì)被用的,那么錯(cuò)誤的使用下標(biāo),將會(huì)導(dǎo)致IndexOutOfBoundsException
越界異常。以下代碼就會(huì)引起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: 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)
3. Log 分析如下:
IndexOutOfBoundsException Log分析
4. IndexOutOfBoundsException 解決方案
在使用時(shí)判斷對(duì)象內(nèi)容是否為0.
使用判斷List 的size是否為0
四、ActivityNotFoundException
ActivityNotFoundException
常見于Eclipse
開發(fā)Android
中,Android studio 已經(jīng)幫忙自動(dòng)生成Activity,以及布局文件。
主要原因是未在AndroidMainfest.xml
文件中注冊(cè),如未注冊(cè),會(huì)引起app crash
,crash log
如下:ActivityNotFoundException: Unable to find explicit activity class
1. ActivityNotFoundException 代碼舉例
比如以下代碼會(huì)引起此異常
Activity未在Androidmainfest.xml 中注冊(cè)會(huì)引起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
中注冊(cè)即可
四大組件一定,一定要在AndroidMainfest.xml 中注冊(cè)
五、IllegalStateException
IllegalStateException
非法狀態(tài)異常,是因?yàn)檐浖写a狀態(tài)非法導(dǎo)致的。
以下代碼會(huì)引起IllegalStateException
。當(dāng)Button
控件聲明android:onClick="IllegalStateException"
卻未在Java
代碼中使用時(shí),點(diǎn)擊Button
,就會(huì)出現(xiàn)此類異常。
1. IllegalStateException 代碼舉例
IllegalStateException 代碼舉例
2. 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)
3. IllegalStateException Log分析如下:
IllegalStateException Log截圖
4. IllegalStateException 解決方案
IllegalStateException
類異常很多,不同的代碼會(huì)有不同的解決方案,上述舉例解決方案如下
IllegalStateException
六、 ArrayIndexOutOfBoundsException 數(shù)組越界異常
數(shù)組在代碼中經(jīng)常被用到,當(dāng)適用數(shù)組下標(biāo)不當(dāng)時(shí),就會(huì)出現(xiàn)ArrayIndexOutOfBoundsException
。比如數(shù)組長(zhǎng)度為4
,但你要引用下標(biāo)為5
的元素,這時(shí)候,就會(huì)異常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=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
3. ArrayIndexOutOfBoundsException Log分析如下:
ArrayIndexOutOfBoundsException Log分析
4. ArrayIndexOutOfBoundsException解決方案
-
1.正確使用數(shù)組下標(biāo)
-
2.如果不確定數(shù)組長(zhǎng)度,請(qǐng)先獲取長(zhǎng)度,然后在判斷下標(biāo)是否大于等于數(shù)組長(zhǎng)度。
-
3.try-catch 抓住異常,防止crash,但不能從根本上解決問題。
七、SecurityException 安全異常
SecurityException
安全異常在Android
中也會(huì)經(jīng)常發(fā)生,主要是Android
的安全機(jī)制原因造成的,為了管理應(yīng)用獲取手機(jī)的一些敏感信息,Android
安全機(jī)制規(guī)定,必須在AndroidMainfest.xml
文件中聲明,并且,Android 6.0
之后,獲取手機(jī)敏感信息時(shí)候,需要?jiǎng)討B(tài)申請(qǐng)權(quán)限,只有用戶授權(quán)后才可以獲取手機(jī)敏感信息。
1. SecurityException 代碼舉例
獲取手機(jī)的IMEI 號(hào)屬于手機(jī)的敏感信息
/**
*
*
*
*
* */
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;
}
}
獲取手機(jī)IMEI號(hào)
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
中申請(qǐng)權(quán)限即可,Android 6.0
之后,請(qǐng)動(dòng)態(tài)申請(qǐng)權(quán)限。
AndroidMainfest.xml 中申請(qǐng)權(quán)限
八、IllegalArgumentException: Service not registered 服務(wù)未注冊(cè)異常
1.報(bào)錯(cuò)信息如下:
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)
2.Log分析如下:
Log 分析
此異常經(jīng)常發(fā)生在錯(cuò)誤的解除綁定服務(wù)造成的,解決方法:
1.解除綁定服務(wù)之前,先判斷是否綁定過,只有綁定過后才可以解綁
2.使用try-catch
抓取住異常
代碼舉例如下:
Service not registered 異常舉例
九、BadTokenException 解決方案
1. log 舉例
03-12 14:55:13.734 5564 5564 E AndroidRuntime: FATAL EXCEPTION: main
03-12 14:55:13.734 5564 5564 E AndroidRuntime: Process: com.android.fmradio, PID: 5564
03-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@b3d2a03
03-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 2003
03-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.產(chǎn)生原因
Android 8.0 之后如果要彈出系統(tǒng)彈窗,需要使用TYPE_APPLICATION_OVERLAY
以及
來進(jìn)行系統(tǒng)彈窗,否則會(huì)報(bào)以下異常BadTokenException: Unable to add window android.view.ViewRootImpl$W@f652dba -- permission denied for window type 2003
3. 解決方案
系統(tǒng)彈窗,請(qǐng)用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 之后彈出系統(tǒng)彈窗,需要使用 TYPE_APPLICATION_OVERLAY
//
// 一下兩個(gè) 之前常用的系統(tǒng)的Dialog 會(huì)報(bào)
// 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 彈窗行為變更
審核編輯 :李倩
-
代碼
+關(guān)注
關(guān)注
30文章
4900瀏覽量
70715 -
數(shù)組
+關(guān)注
關(guān)注
1文章
420瀏覽量
26540
原文標(biāo)題:九、BadTokenException 解決方案
文章出處:【微信號(hào):哆啦安全,微信公眾號(hào):哆啦安全】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄


#硬聲創(chuàng)作季 #FPGA Xilinx入門-16 亞穩(wěn)態(tài)現(xiàn)象原理與解決方案-1

#硬聲創(chuàng)作季 #FPGA Xilinx入門-16 亞穩(wěn)態(tài)現(xiàn)象原理與解決方案-2

#硬聲創(chuàng)作季 #FPGA Xilinx入門-16 亞穩(wěn)態(tài)現(xiàn)象原理與解決方案-3

#硬聲創(chuàng)作季 #FPGA Xilinx入門-16 亞穩(wěn)態(tài)現(xiàn)象原理與解決方案-4

#硬聲創(chuàng)作季 #FPGA Xilinx入門-16 亞穩(wěn)態(tài)現(xiàn)象原理與解決方案-5

#硬聲創(chuàng)作季 云計(jì)算基礎(chǔ)入門:18-rpm痛點(diǎn)及解決方案

升降柱安裝工藝:如何調(diào)平,你們平時(shí)都用什么工具#施工現(xiàn)場(chǎng) #升降柱 #出入口解決方案#硬聲創(chuàng)作季

#硬聲創(chuàng)作季 計(jì)算概論與程序設(shè)計(jì)基礎(chǔ):沒有解決方案就沒有程序

#硬聲創(chuàng)作季 #FPGA FPGA-17-02 按鍵抖動(dòng)現(xiàn)象介紹與解決方案分析-1

#硬聲創(chuàng)作季 #FPGA FPGA-17-02 按鍵抖動(dòng)現(xiàn)象介紹與解決方案分析-2

評(píng)論