首页 > 编程语言 >调用fragment中的onActivityResult时发生java.lang.IndexOutOfBoundsException问题,请问下什么原因?

调用fragment中的onActivityResult时发生java.lang.IndexOutOfBoundsException问题,请问下什么原因?

时间:2023-01-19 17:11:34浏览次数:51  
标签:lang 20 48 fragment 44.103 10982 15 java AndroidRuntime

调用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 即可。

标签:lang,20,48,fragment,44.103,10982,15,java,AndroidRuntime
From: https://www.cnblogs.com/dituirenwu/p/17061810.html

相关文章

  • JavaWeb-JSP
    JavaWeb-JSP1,JSP概述JSP(全称:JavaServerPages):Java服务端页面。是一种动态的网页技术,其中既可以定义HTML、JS、CSS等静态内容,还可以定义Java代码的动态内容,也就是JS......
  • 【学懂Java】(四)面向对象编程-2
    一.局部变量和成员变量局部变量成员变量(全局变量)定义在方法中定义在方法外,类之内的变量栈内存中堆内存中局部变量没有默认值成员变量有默认值当前方法当前类的方法不同的方......
  • Golang的基本数据类型-基本使用
    基本数据类型-基础使用数值型整数类型使用细节Golang各整数类型分:有符号和无符号,intuint的大小和系统是32位还是64位有关Golang的整型默认声明为int型如何......
  • java学习之生成CSV文件
    1.pom.xml文件 2.代码  ......
  • kettle 创建资源库 Java api DatabaseRepository
    创建资源库前置mysql数据库test核心代码KettleEnvironment.init();DatabaseMetadatabaseMeta=newDatabaseMeta("test-db","MYSQL","Native",......
  • 【学懂Java】(四)面向对象编程-1
    一.OOP:ObjectOrientedProgramming1.面向过程和面向对象面向过程面向对象区别事物比较简单,可以用线性的思维去解决事物比较复杂,使用简单的线性思维无法解决共同点1.面向......
  • java.lang.UnsupportedClassVersionError
    一般是版本不兼容Exceptioninthread"main"java.lang.UnsupportedClassVersionError:org/eclipse/swt/events/ShellListenerhasbeencompiledbyamorerecentver......
  • Java零基础09篇:while循环和案例
    文章目录​​一、while结构(掌握)​​​​while循环完整格式:​​​​while循环执行流程:​​​​示例代码:​​​​二、while循环练习-珠穆朗玛峰(应用)​​​​需求:世界最高山峰......
  • Java零基础07篇:switch语句以及案例
    文章目录​​一、switch语句结构(本阶段需要掌握)​​​​格式:​​​​执行流程:​​​​二、小练习-春夏秋冬​​​​需求:一年12个月分属于春夏秋冬4个季节,键盘录入一个月份,用......
  • Java零基础06篇:流程控制语句应用篇
    文章目录​​前言​​​​一、流程控制语句分类​​​​二、顺序结构​​​​三、分支结构之if语句​​​​if语句格式1:​​​​if语句格式2:​​​​if语句案例:奇偶数​​​......