【问题标题】:App crashes on adding second navHostFragment应用程序在添加第二个 navHostFragment 时崩溃
【发布时间】:2020-09-10 12:21:12
【问题描述】:

由于某种情况,我需要在我的应用程序的两个不同活动中使用两个 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

以下是我的第二个 navhost 片段:

    <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" />

请帮忙。

【问题讨论】:

  • 你能把你所有的xml发上来吗?

标签: java android android-layout kotlin android-fragments


【解决方案1】:

问题在于您使用片段>的方式。 您可以在 XML 中包含 fragment> 以在布局中嵌入片段实例。 我可以建议为您的片段创建一个全新的布局,而不是使用片段>。 编辑 创建一个单独的布局,然后将您的片段实例嵌入其中,例如:

<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>

【讨论】:

  • 能否详细说明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多