导航目标 com.example.multimodulepoc:id/navigation_home 不是此 NavGraph 的直接子级

发布于 2025-01-12 22:51:19 字数 2274 浏览 1 评论 0原文

我正在尝试使用我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢欲 2025-01-19 22:51:19

您的主模块图具有:

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

Your home module graph has:

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

So the ID of your home graph is @id/home_navigation but that's not the ID you've set as the startDestination of your app level graph, hence the error message:

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

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 the android:id of your home graph:

<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>
独行侠 2025-01-19 22:51:19

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

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)

I'faced similar crash if trying to navigate another module 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 

With the help of @ianhanniballake comments, Fixed the crash with following code helps me to resolve the crash.

findNavController().setGraph(R.navigation.dashboard_navigation)
findNavController().navigate(R.id.navigation_dashboard)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文