英文:
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:
<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" />
Please help.
答案1
得分: 1
问题在于你使用了<fragment>。你可以在XML中使用<fragment>来嵌入布局内的片段实例。
我可以建议的是为你的片段创建一个全新的布局,而不使用<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>
英文:
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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论