调用fragment中的onActivityResult时概率发生java.lang.IndexOutOfBoundsException问题,请问下什么原因?
StartScanCodeFragment中代码:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
OCarLog.d(TAG, "onActivityResult: requestCode = $requestCode, resultCode = $resultCode")
// 扫描结果回调
if (requestCode == SettingsUtil.REQUEST_QR_CODE && resultCode == AppCompatActivity.RESULT_OK) {
// 振动提示
beepManager.playBeepSoundAndVibrate()
// 将扫描出的信息显示出来
val bundle = data?.extras
val scanResult = bundle?.getString(SettingsUtil.SCAN_RESULT_STRING) ?: return
OCarLog.d(TAG, "scanResult = $scanResult")
if (!scanResult.split("?")[0].contains(SettingsUtil.YI_LIAN_QR_CODE_PREFIX)) {
val message = StringBuilder(getString(R.string.scan_code_fail_tips)).append("\n\n").toString()
context?.let {
SettingsUtil.get()
.makeTwoButtonDialog(requireContext(), getString(R.string.scan_code_fail),
message,
getString(R.string.restart_scan),
getString(R.string.o_car_dialog_bt_dismiss),
onPositiveButtonClick = {
startQrCode()
OCarLog.d(TAG, "Go to rescan page.")
},
onNegativeButtonClick = {
OCarLog.d(TAG, "cancel.")
}).show()
}
return
}
}
activity中写了一个空的重载,调用了父类的onActivityResult:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
}
出现问题的堆栈:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=58021756, result=-1, data=Intent { (has extras) }} to activity {com.oplus.ocar/com.oplus.ocar.settings.connect.EcScanConnectActivity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:5517)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:5556)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:54)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2348)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:233)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.os.Looper.loop(Looper.java:344)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:8191)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at java.util.Collections$SingletonList.get(Collections.java:4872)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at com.oplus.ocar.settings.connect.StartScanCodeFragment.onActivityResult(SourceFile:31)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.fragment.app.FragmentManager$9.onActivityResult(SourceFile:10)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.fragment.app.FragmentManager$9.onActivityResult(SourceFile:1)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.activity.result.ActivityResultRegistry.doDispatch(SourceFile:3)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.activity.result.ActivityResultRegistry.dispatchResult(SourceFile:3)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.activity.ComponentActivity.onActivityResult(SourceFile:1)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at androidx.fragment.app.FragmentActivity.onActivityResult(SourceFile:2)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at com.oplus.ocar.settings.connect.EcScanConnectActivity.onActivityResult(SourceFile:1)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:8487)
01-15 20:48:44.103 10982 10982 E AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:5510)
不知道SingletonList.get是那里调的,出现了数组越界,有谁帮忙看下,感谢!!
解答:
scanRresult.split("?") 数组长度应该为 0,你这个时候使用 scanRresult.split("?")[0]就出现数组越界异常了!!!
判断一下 scanRresult.split("?") 数组长度大于 0 即可。