我正在使用Android的导航组件(androidx.navigation
),并且还有一个底部导航菜单,最初包含3个项目(并且,我的导航图布局最初具有3个目的地)。但是在某些情况下,我以编程方式在底部导航菜单中创建了第4个项目,同时我向导航图添加了新的目的地:
val navController = Navigation.findNavController(this, R.id.nav_host)
navController.graph.addDestination(
FragmentNavigator(this@MainActivity, supportFragmentManager, R.id.nav_host)
.createDestination()
.apply {
id = R.id.action_forth
className = "package.MyForthFragment"
label = getString(R.string.res_4th_label)
})
bottomNavigationView.menu
.add(Menu.NONE, R.id.action_forth, FORTH_ITEM_POSITION, R.string.res_4th_label)
.setIcon(R.drawable.ic_bottom_nav_4_item)
.setOnMenuItemClickListener {
someAction()
true
}
一切正常,但是如果我旋转设备,则应用程序崩溃并出现以下错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{package.MainActivity}: android.view.InflateException: Binary XML file line #46: Binary XML file line #46: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4919)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: android.view.InflateException: Binary XML file line #46: Binary XML file line #46: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class fragment
Caused by: java.lang.IllegalStateException: unknown destination during restore: package:id/action_forth
at androidx.navigation.NavController.onGraphCreated(NavController.java:506)
at androidx.navigation.NavController.setGraph(NavController.java:482)
at androidx.navigation.NavController.setGraph(NavController.java:447)
at androidx.navigation.NavController.setGraph(NavController.java:429)
at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:226)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1684)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3774)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at package.MainActivity.onCreate(MainActivity.kt:162)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4919)
at android.app.ActivityThread.-wrap19(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1702)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
如何解决此问题?