添加第二个navHostFragment时应用程序崩溃



由于一个条件,我的应用程序中的两个不同活动中需要两个navHostFragment。在我的应用程序中添加第二个navhostframent之前,一切都很好,这会创建一个inflateexception,并出现错误:

Binary XML file line #36: Duplicate id 0x7f09018a, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment

以下是我的第二个导航主机片段:

<fragment
android:id="@+id/nav_host_fragment_supervisor"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNav"
android:layout_below="@+id/appbar"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph_supervisor" />

请帮忙。

问题在于您使用<片段>。您可以包括<片段>以在布局中嵌入片段实例。我可以建议为您的片段创建一个全新的布局,而不要使用<片段>。EDIT创建一个单独的布局,然后将您的片段实例嵌入其中,例如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/nav_host_fragment_supervisor"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNav"
android:layout_below="@+id/appbar"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph_supervisor" />
</FrameLayout>

最新更新