您必须在调用 getGraph() 之前调用 setGraph()
You must call setGraph() before calling getGraph()
我有底部导航我也用 FragmentContainerView
替换了 <fragment>
标签,它给我错误关于它没有设置 NavController,然后我被发布 问题并解决了问题,但是当方向改变并在底部导航中选择项目时我得到了以下错误
java.lang.IllegalStateException: You must call setGraph() before calling getGraph()
片段容器
<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" /
寻找片段
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
。
使用导航 UI 的最新版本:
// navigation
implementation "androidx.navigation:navigation-fragment:2.3.0-alpha02"
implementation "androidx.navigation:navigation-ui:2.3.0-alpha02"
它修复了新版本
按照此 link 解决此问题
https://developer.android.com/jetpack/androidx/releases/navigation
简单来说,将您的依赖项升级到应用程序模块
中的以下依赖项
def nav_version = "2.3.0-alpha05"
// Java
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
//科特林
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//动态特性模块支持
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// 测试导航
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
如果您使用安全参数,则在依赖项下的项目模块中
def nav_version = "2.3.0-alpha05"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
解决了我的问题..
如果您像我一样忘记添加到您的FragmentContainerView
,您会遇到同样的错误
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
我有底部导航我也用 FragmentContainerView
替换了 <fragment>
标签,它给我错误关于它没有设置 NavController,然后我被发布
java.lang.IllegalStateException: You must call setGraph() before calling getGraph()
片段容器
<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" /
寻找片段
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
。
使用导航 UI 的最新版本:
// navigation
implementation "androidx.navigation:navigation-fragment:2.3.0-alpha02"
implementation "androidx.navigation:navigation-ui:2.3.0-alpha02"
它修复了新版本
按照此 link 解决此问题 https://developer.android.com/jetpack/androidx/releases/navigation 简单来说,将您的依赖项升级到应用程序模块
中的以下依赖项def nav_version = "2.3.0-alpha05"
// Java
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
//科特林
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//动态特性模块支持
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// 测试导航
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
如果您使用安全参数,则在依赖项下的项目模块中
def nav_version = "2.3.0-alpha05"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
解决了我的问题..
如果您像我一样忘记添加到您的FragmentContainerView
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"