我有底部导航,我也替换了 <fragment>
标记为 FragmentContainerView
它给了我关于它没有设置 NavController 的错误,然后我被发布 this问题并解决了问题,但是当方向更改并在底部导航中选择项目时,我收到以下错误
java.lang.IllegalStateException: You must call setGraph() before calling getGraph()
fragment 容器
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" /
寻找 fragment
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_classes, R.id.navigation_schedule, R.id.navigation_settings
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
最佳答案
这是根据 2.2.0-rc03 release notes 修复的:
NavHostFragment
now correctly restores the graph after a configuration change when used withFragmentContainerView
. (b/143752103)
因此请确保您使用的是 2.2.0-rc03
。
关于android - 调用 getGraph() 之前必须先调用 setGraph(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290968/