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

huangapple go评论76阅读模式
英文:

App crashes on adding second navHostFragment

问题

由于某种情况,我在我的应用程序中需要在两个不同的活动中使用两个不同的navHostFragment。在我添加第二个navhostframent到我的应用程序后,一切都正常工作,但是会出现inflateexception错误:

二进制 XML 文件第 36 行:重复的 id 0x7f09018a、标记 null,或父 id 0xffffffff,与另一个片段冲突,为 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" />

请提供帮助。

英文:

Due to a condition, I need two navHostFragment in two different activities in my app.
Everything works fine until I add a second navhostframent in my app, which creates an inflateexception with error:

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

Following is my second navhost fragment:

    &lt;fragment
                android:id=&quot;@+id/nav_host_fragment_supervisor&quot;
                android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;match_parent&quot;
                android:layout_above=&quot;@+id/bottomNav&quot;
                android:layout_below=&quot;@+id/appbar&quot;
                app:defaultNavHost=&quot;true&quot;
                app:navGraph=&quot;@navigation/navigation_graph_supervisor&quot; /&gt;

Please help.

答案1

得分: 1

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

&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/fragment_container&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; &gt;

     &lt;fragment
            android:id=&quot;@+id/nav_host_fragment_supervisor&quot;
           android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_above=&quot;@+id/bottomNav&quot;
            android:layout_below=&quot;@+id/appbar&quot;
            app:defaultNavHost=&quot;true&quot;
            app:navGraph=&quot;@navigation/navigation_graph_supervisor&quot; /&gt;

&lt;/FrameLayout&gt;
英文:

The problem is the way you have used <fragment>.
You can include <fragment> in XML to embed fragment instance inside of the layout.
What I can suggest is to create a whole new layout for your fragment and not use <fragment>.
EDIT
Create a separate layout and then embed your fragment instance inside it for example:

&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:id=&quot;@+id/fragment_container&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot; &gt;

     &lt;fragment
            android:id=&quot;@+id/nav_host_fragment_supervisor&quot;
           android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;match_parent&quot;
            android:layout_above=&quot;@+id/bottomNav&quot;
            android:layout_below=&quot;@+id/appbar&quot;
            app:defaultNavHost=&quot;true&quot;
            app:navGraph=&quot;@navigation/navigation_graph_supervisor&quot; /&gt;

&lt;/FrameLayout&gt;

huangapple
  • 本文由 发表于 2020年9月10日 20:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63829567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定