Navigation destination com.example.multodulepoc:id/Navigatio



我正试图使用我从home模块提供的startDestination标签来添加应用程序中多个模块的导航。当我只有一个带有startDestination的<include>标记时,它可以正常工作。当我添加多个模块导航时,应用程序崩溃,出现以下错误消息。我在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>

您的主模块图具有:

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

因此,主图的ID是@id/home_navigation,但这不是您设置为应用程序级图的startDestination的ID,因此出现错误消息:

navigation destination com.example.multimodulepoc:id/navigation_home is not a direct child of this NavGraph 

"直接子"是这里最重要的部分:您的应用程序级图形不知道主图形中的目的地,只知道图形本身。因此,您需要确保应用程序级图形的startDestination与主图形的android:id实际匹配:

<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/home_navigation">
<include app:graph="@navigation/home_navigation" />
<include app:graph="@navigation/dashboard_navigation" />
</navigation>

如果我试图导航另一个模块导航,我也会遇到类似的崩溃。

java.lang.IllegalArgumentException: Navigation action/destination com.example.multimodulepoc:id/navigation_dashboard cannot be found from the current destination Destination(com.example.multimodulepoc:id/navigation_home) label=Home class=com.example.home.home.HomeFragment 

在@ianhanniballake评论的帮助下,用以下代码修复了崩溃,帮助我解决了崩溃。

findNavController().setGraph(R.navigation.dashboard_navigation)
findNavController().navigate(R.id.navigation_dashboard)

最新更新