本文主要是介绍Android DataBinding报错:java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment 更换成 Fragm,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android DataBinding报错:java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment 更换成 FragmentContainerView
- 前言
- BUG
- 更换FragmentContainerView
- 混淆
- 完事
前言
今天修复BUG的时候,顺带升级了一下,项目关联的库,升级后突然出现在DataBinding的报错,竟然还是java.lang.ArrayIndexOutOfBoundsException: length=2; index=2,竟然还是个列表位置错误,???满头问号
BUG
E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.xxx, PID: 27182java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx/com.xxx.activity.main.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3898)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462)at android.os.Handler.dispatchMessage(Handler.java:110)at android.os.Looper.loop(Looper.java:219)at android.app.ActivityThread.main(ActivityThread.java:8393)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)Caused by: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1211)at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1268)at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1268)at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1268)at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:1268)at androidx.databinding.ViewDataBinding.mapBindings(ViewDataBinding.java:719)at com.xxx.databinding.ActivityMainBindingImpl.<init>(ActivityMainBindingImpl.java:28)at com.xxx.DataBinderMapperImpl.internalGetViewDataBinding0(DataBinderMapperImpl.java:378)at com.xxx.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:663)at androidx.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)at androidx.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)at androidx.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:327)at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:306)at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)at com.xxx.activity.main.MainActivity.onCreate(MainActivity.kt:72)at android.app.Activity.performCreate(Activity.java:8121)at android.app.Activity.performCreate(Activity.java:8109)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3871)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8393) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
经过排查我怀疑时页面中使用了Fragment的问题,因为启动其他不带这个的页面Databinding就不会出现刚刚的问题;
点开页面布局 XML,就能看到提示:
Replace the < fragment > tag with FragmentContainerView.
更换FragmentContainerView
根据提示将 fragment 更换成 FragmentContainerView,重新运行,不再报“java.lang.ArrayIndexOutOfBoundsException: length=2; index=2”错误了,但是报了个新错误:
E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.xxx, PID: 27863java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx/com.xxx.activity.main.MainActivity}: java.lang.IllegalStateException: Activity com.xxx.activity.main.MainActivity@1f5b1e does not have a NavController set on 2131296506at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3898)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462)at android.os.Handler.dispatchMessage(Handler.java:110)at android.os.Looper.loop(Looper.java:219)at android.app.ActivityThread.main(ActivityThread.java:8393)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)Caused by: java.lang.IllegalStateException: Activity com.xxx.activity.main.MainActivity@1f5b1e does not have a NavController set on 2131296506at androidx.navigation.Navigation.findNavController(Navigation.java:61)at com.xxx.activity.main.MainActivity.onCreate(MainActivity.kt:75)at android.app.Activity.performCreate(Activity.java:8121)at android.app.Activity.performCreate(Activity.java:8109)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3871)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4081) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2462) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8393) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
这个问题就报在了获取navController中,在stackoverflow上搜索了一下,就找到了FragmentContainerView获取navController的新方法
参考:https://stackoverflow.com/questions/64170603/replace-the-fragment-tag-with-fragmentcontainerview
// 适配 FragmentContainerView
val navHostFragment = supportFragmentManager.findFragmentById(R.id.fragment) as NavHostFragment
navController = navHostFragment.navController
// 适配 Fragment
//navController = Navigation.findNavController(this, R.id.fragment)// 设置tab栏与fragment绑定
NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
混淆
-keepnames class androidx.navigation.fragment.NavHostFragment
-keepnames class com.google.android.gms.maps.SupportMapFragment
完事
重新运行,问题解决。
这篇关于Android DataBinding报错:java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment 更换成 Fragm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!