android - 调用 getGraph() 之前必须先调用 setGraph()

标签 android android-fragments bottomnavigationview orientation-changes

我有底部导航,我也替换了 <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 with FragmentContainerView. (b/143752103)

因此请确保您使用的是 2.2.0-rc03

关于android - 调用 getGraph() 之前必须先调用 setGraph(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290968/

相关文章:

java - 如何从 firebase 检索数据并将其放入文本字​​段

java - 过渡 fragment 调用退出过渡但动画没有发生

java - 使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本

android - 是否有记录在案的 Android 版本接收安全更新的生命周期?

android - 实现需要上下文的 Parcelable 类

java - 从 fragment 移回到上一个 Activity

android - 如何动态更改底部导航项文本?

java - Android如何在单击按钮时更改底部导航项的颜色

选择底部导航选项卡后Android清除回栈

android - 无法运行程序 "/data/data/my.app.package/files/my-executable": error=13, 权限被拒绝