You must call setGraph() before calling getGraph()

会有一股神秘感。 提交于 2020-04-10 04:49:48

问题


I have bottom Navigation also I'm replacing the <fragment> tag with FragmentContainerView it gives me error about does not have a NavController set on, then i was posted this question and fixed the problem, but I got this below error when orientation is changed and selecting item in Bottom Navigation

java.lang.IllegalStateException: You must call setGraph() before calling getGraph()

FragmentContainer

<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" /

finding 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)

回答1:


This is fixed as per the 2.2.0-rc03 release notes:

NavHostFragment now correctly restores the graph after a configuration change when used with FragmentContainerView. (b/143752103)

So make sure you are using 2.2.0-rc03.




回答2:


use Navigation UI's latest vesion:

// navigation
implementation "androidx.navigation:navigation-fragment:2.3.0-alpha02"
implementation "androidx.navigation:navigation-ui:2.3.0-alpha02"

it fixed on new versions



来源:https://stackoverflow.com/questions/59290968/you-must-call-setgraph-before-calling-getgraph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!