Android databinding error: Java. Lang. ArrayIndexOutOfBoundsException: length = 2; Index = 2 replace fragment with fragmentcontainerview
Preface bug replacing fragmentcontainerview
preface
When fixing the bug today, I upgraded the library associated with the project. After upgrading, an error appears in the databinding. Unexpectedly, it is still java.lang.arrayindexoutofboundsexception: length = 2; Index = 2, it’s still a list position error,???Full of question marks
BUG
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx, PID: 27182
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx/com.xxx.activity.main.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at 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=2
at 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)
After troubleshooting, I suspect that the fragment is used in the page, because the problem will not occur when starting other pages without this data binding;
Click the page layout XML to see the prompt:
replace the & lt; fragment > tag with FragmentContainerView.
Replace fragmentcontainerview
Replace the fragment with fragmentcontainerview according to the prompt, run it again, and no longer report “Java. Lang. ArrayIndexOutOfBoundsException: length = 2; “Index = 2” is wrong, but a new error is reported:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx, PID: 27863
java.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 2131296506
at 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 2131296506
at 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)
This problem is reported in obtaining the navcontroller. After searching the StackOverflow, we found the new method of fragmentcontainerview to obtain the navcontroller
reference resources: 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)
// Set the tab bar to bind to the fragment
NavigationUI.setupWithNavController(binding.bottomNavigationView, navController)
confusion
-keepnames class androidx.navigation.fragment.NavHostFragment
-keepnames class com.google.android.gms.maps.SupportMapFragment
Finish
Re-run and solve the problem.