app 常見 Crash 案例匯總
共 18211字,需瀏覽 37分鐘
·
2024-07-11 07:42
和你一起終身學(xué)習(xí),這里是程序員Android
經(jīng)典好文推薦,通過閱讀本文,您將收獲以下知識點:
一、NullPointerException 空指針
二、ClassCastException 類型轉(zhuǎn)換異常
三、IndexOutOfBoundsException 下標(biāo)越界異常
四、ActivityNotFoundException Activity未找到異常
五、IllegalStateException 非法狀態(tài)異常
六、ArrayIndexOutOfBoundsException 數(shù)組越界異常
七、SecurityException 安全異常
八、llegalArgumentException: Service not registered 服務(wù)未注冊異常
九、BadTokenException:
Exception 在Android 中經(jīng)常會遇到,那么遇到異常我們該如何解決,本文將舉例解決部分Android看法中遇到的異常。
一、NullPointerException 空指針
NullPointerException在開發(fā)中經(jīng)常會碰到,比如引用的對象為空,數(shù)組為空等等都會引起空指針異常,如不及時處理,就會導(dǎo)致 應(yīng)用Crash。
1. 數(shù)組 NullPointerException
不能向一個null數(shù)組元素賦值,獲取長度,否則報NullPointerException: Attempt to write to null array和NullPointerException Attempt to get length of null array,以下代碼會引起上面兩種空指針異常。
2. 數(shù)組NullPointerException 代碼舉例
public static void ArrayNullPointer() {
/**
* 數(shù)組空指針 NullPointerException
*
* 1.獲取null數(shù)組長度
* 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ù)組長度導(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)容,如果獲取則會導(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 對象 NullPointerException
對象空指針,這個是常見的空指針,主要是因為引用一個null 對象,進(jìn)而導(dǎo)致空指針,常報以下錯誤Attempt to invoke a virtual method on a null object reference,以下代碼可能會引起空指針異常。
6. object 對象 NullPointerException 代碼舉例
簡單代碼舉例如下:
public static void ListNullPointer() {
ArrayList<String> 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 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 對象 NullPointerException Log 分析如下:
Object NullPointerException
9. NullPointerException 解決方案
1.使用時多注意判斷對象是否為空
規(guī)避空指針舉例如下:
public static void ListNullPointer() {
ArrayList<String> mArrayList = null;
if (mArrayList != null) {
mArrayList.size();
}
}
使用對象是,最好判斷對象是否為空
2.使用
try-catch將拋出的異常抓住
try-catch 可以抓住拋出的異常,使應(yīng)用程序不崩潰,但是,這個不是從根本上解決問題,會引起一些莫名其妙的問題。
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)換時,并且在編譯期間,編譯器不會提示報錯,但是當(dāng)運行時,如果存在此異常,可能會導(dǎo)致app崩潰 crash。
比如當(dāng)父類強(qiáng)制轉(zhuǎn)換為子類時,ClassCastException 就會發(fā)生
1. 以下代碼 會引起 ClassCastException
請勿 父類強(qiáng)制轉(zhuǎn)換為子類,否則就會發(fā)生ClassCastException異常。
public void ClassCastExample() {
Fruit banana = new Fruit();
/**
* ClassCastException
*
* 1. 此處強(qiáng)制轉(zhuǎn)換,會導(dǎo)致 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. 此處強(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通常會打印以下類似信息
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 測試Settings模塊時,報出的 ClassCastException,Settings代碼比較多,一時也無法看完,此時,try-catch 也是一種不錯的選擇。
比如monkey測試某平臺代碼時,報出以下異常
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)
6. Settings ClassCastException Log分析
Settings ClassCastException Log1
Settings ClassCastException Log2
7. Setting crash ClassCastException 解決方案:
try-catch 異常報錯的地方
try-catch 異常報錯的地方
try-catch 異常報錯的地方
三、IndexOutOfBoundsException 下標(biāo)越界異常
List 在開發(fā)中經(jīng)常會被用的,那么錯誤的使用下標(biāo),將會導(dǎo)致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: 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 解決方案
在使用時判斷對象內(nèi)容是否為0.
使用判斷List 的size是否為0
四、ActivityNotFoundException
ActivityNotFoundException 常見于Eclipse 開發(fā)Android中,Android studio 已經(jīng)幫忙自動生成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 非法狀態(tài)異常,是因為軟件中代碼狀態(tài)非法導(dǎo)致的。
以下代碼會引起IllegalStateException 。當(dāng)Button控件聲明android:onClick="IllegalStateException" 卻未在Java代碼中使用時,點擊Button,就會出現(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 類異常很多,不同的代碼會有不同的解決方案,上述舉例解決方案如下
IllegalStateException
六、 ArrayIndexOutOfBoundsException 數(shù)組越界異常
數(shù)組在代碼中經(jīng)常被用到,當(dāng)適用數(shù)組下標(biāo)不當(dāng)時,就會出現(xiàn)ArrayIndexOutOfBoundsException。比如數(shù)組長度為4,但你要引用下標(biāo)為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=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ù)組長度,請先獲取長度,然后在判斷下標(biāo)是否大于等于數(shù)組長度。
3.try-catch 抓住異常,防止crash,但不能從根本上解決問題。
七、SecurityException 安全異常
SecurityException 安全異常在Android 中也會經(jīng)常發(fā)生,主要是Android 的安全機(jī)制原因造成的,為了管理應(yīng)用獲取手機(jī)的一些敏感信息,Android安全機(jī)制規(guī)定,必須在AndroidMainfest.xml文件中聲明,并且,Android 6.0之后,獲取手機(jī)敏感信息時候,需要動態(tài)申請權(quán)限,只有用戶授權(quán)后才可以獲取手機(jī)敏感信息。
1. SecurityException 代碼舉例
獲取手機(jī)的IMEI 號屬于手機(jī)的敏感信息
/**
*
* <!-- 讀取手機(jī)IMEI的設(shè)備權(quán)限 -->
*
* <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;
}
}
獲取手機(jī)IMEI號
2. 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)
3. SecurityException log 分析
SecurityException log 分析
4. SecurityException 解決方案
Android 6.0之前,在AndroidMainfest.xml中申請權(quán)限即可,Android 6.0 之后,請動態(tài)申請權(quán)限。
AndroidMainfest.xml 中申請權(quán)限
八、IllegalArgumentException: Service not registered 服務(wù)未注冊異常
1.報錯信息如下:
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ā)生在錯誤的解除綁定服務(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以及<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />來進(jìn)行系統(tǒng)彈窗,否則會報以下異常BadTokenException: Unable to add window android.view.ViewRootImpl$W@f652dba -- permission denied for window type 2003
3. 解決方案
系統(tǒ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
// <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
// 一下兩個 之前常用的系統(tǒng)的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 彈窗行為變更
至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請聯(lián)系小編刪除。同時感謝您的閱讀,期待您的關(guān)注。
點個在看,方便您使用時快速查找!
