似乎无法立即使用FragmentContainerView吗?

<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的代码,但我总是总是收到此错误:
Caused by: java.lang.IllegalStateException: Activity ...MainActivity@797467d does not have a NavController set on 2131296504

仅使用<fragment>作品和AFAIK,就应该将其替换为FragmentContainerView

我是否缺少某些东西,还是有人可以将FragmentContainerView用作NavHostFragment

非常感谢!

最佳答案

由于此错误报告:
https://issuetracker.google.com/issues/142847973

(当前)唯一的方法是:

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();

07-24 13:12