首页 > 解决方案 > 使用 FragmentContainerView 和 NavHostFragment 时出错

问题描述

这是我在 xml 中的代码,

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_auth_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph_auth" />

我有例外

Caused by: android.view.InflateException: Binary XML file line #9 in activity_auth: Binary XML file line #9 in activity_auth: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: android.view.InflateException: Binary XML file line #9 in activity_auth: Error inflating class androidx.fragment.app.FragmentContainerView
 Caused by: java.lang.IllegalStateException: FragmentContainerView must have an android:id to add Fragment androidx.navigation.fragment.NavHostFragment

请帮助我,谢谢!

标签: androidnavigationandroid-jetpack

解决方案


推荐阅读