导航目标 com.example.multimodulepoc:id/navigation_home 不是此 NavGraph 的直接子级
我正在尝试使用我从 home
模块提供的 startDestination 的 标签在应用程序中添加多个模块的导航。当我只有一个带有 startDestination 的
标记时,它工作正常。当我添加多个模块导航时,应用程序崩溃并出现以下错误消息。我在 SO 中没有找到有用的链接。请帮助我们解决这个问题。提前致谢。
Caused by: java.lang.IllegalArgumentException: navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph
下面是我的导航 xml 和依赖项。
app build.gradle.
dependencies {
....
implementation project( ':home')
implementation project( ':Dashobard')
....
}
应用程序级导航 xml。
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/navigation_home">
<include app:graph="@navigation/home_navigation" />
<include app:graph="@navigation/dashboard_navigation" />
</navigation>
首页模块导航
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.ns.mpos.home.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
</navigation>
仪表板导航
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dashboard_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.ns.mpos.dashobard.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
</navigation>
I'm trying to add navigation of multiple modules in the app using tag I've provided my startDestination from home
module. Its working fine when I've only one <include>
tag with startDestination. App getting crash with following error message when I added multiple module navigations. I found no useful links in SO. Please help us to resolve this issue. Thanks in advance.
Caused by: java.lang.IllegalArgumentException: navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph
Below is my navigation xml and dependency.
app build.gradle.
dependencies {
....
implementation project( ':home')
implementation project( ':Dashobard')
....
}
App level Navigation xml.
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/navigation_home">
<include app:graph="@navigation/home_navigation" />
<include app:graph="@navigation/dashboard_navigation" />
</navigation>
Home module Navigation
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.ns.mpos.home.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" />
</navigation>
Dashboard Navigation
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dashboard_navigation"
app:startDestination="@+id/navigation_dashboard">
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.ns.mpos.dashobard.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
</navigation>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的主模块图具有:
因此您的主图的 ID 是
@id/home_navigation
但这不是您设置为应用级图的startDestination
的 ID,因此错误消息:“直接子级”是这里最重要的部分:您的应用程序级别图表不了解主图表中的目的地,仅了解图表本身。因此,您需要确保应用级图表的
startDestination
实际上与主图表的android:id
匹配:Your home module graph has:
So the ID of your home graph is
@id/home_navigation
but that's not the ID you've set as thestartDestination
of your app level graph, hence the error message:The 'direct child' is the most important part here: your app level graph has no knowledge of the destinations within your home graph, just the graph itself. Therefore, you need to ensure that the
startDestination
of your app level graph actually matches theandroid:id
of your home graph:如果尝试导航另一个模块导航,我遇到类似的崩溃。
在 @ianhanniballake 评论的帮助下,使用以下代码修复崩溃可以帮助我解决崩溃问题。
I'faced similar crash if trying to navigate another module navigation.
With the help of @ianhanniballake comments, Fixed the crash with following code helps me to resolve the crash.