本文介绍了FragmentContainerView作为NavHostFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好像不能立即使用FragmentContainerView
一样工作吗?
Seems like using the FragmentContainerView
doesn't work right out of the box?
<androidx.fragment.app.FragmentContainerView
class="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/fragment_nav_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_app" />
这是我使用fragment-ktx:1.2.0-rc01
的代码,而我总是总是收到此错误:
Here's my code using fragment-ktx:1.2.0-rc01
and I'm just always getting this error:
Caused by: java.lang.IllegalStateException: Activity ...MainActivity@797467d does not have a NavController set on 2131296504
只需使用<fragment>
作品和AFAIK,就应该将其替换为FragmentContainerView
.
Just using <fragment>
works and AFAIK, it's just supposed to be replaced by FragmentContainerView
.
我错过了什么吗?还是有人可以将FragmentContainerView
用作NavHostFragment
?
Am I missing something or was anyone able to use FragmentContainerView
as a NavHostFragment
?
非常感谢!
推荐答案
由于此错误报告: https://issuetracker.google.com/issues/142847973
(当前)唯一的方法是:
The only way (currently) is to to this:
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
(Java):
NavHostFragment navHostFragment =
(NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.my_nav_host_fragment);
NavController navController = navHostFragment.getNavController();
这篇关于FragmentContainerView作为NavHostFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!