android runtime_error ai_generated true

IllegalArgumentException: navigation destination com.example:id/destHome is not found in NavController

ID: android/navigation-destination-not-found

Also available as: JSON · Markdown · 中文
85%Fix Rate
85%Confidence
1Evidence
2023-05-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
AndroidX Navigation 2.5.3 active
AndroidX Navigation 2.6.0 active

Root Cause

The navigation action references a destination ID that is not declared in the associated NavGraph, often due to a missing fragment or a typo in the navigation XML.

generic

中文

导航操作引用了未在关联的 NavGraph 中声明的目的地 ID,通常是由于缺少片段或导航 XML 中的拼写错误。

Official Documentation

https://developer.android.com/guide/navigation/navigation-navigate

Workarounds

  1. 85% success Ensure the destination ID is declared in the correct NavGraph file (e.g., nav_graph.xml) and that the action's destination attribute matches exactly. Example: <fragment android:id="@+id/destHome" android:name="com.example.HomeFragment" />
    Ensure the destination ID is declared in the correct NavGraph file (e.g., nav_graph.xml) and that the action's destination attribute matches exactly. Example: <fragment android:id="@+id/destHome" android:name="com.example.HomeFragment" />
  2. 90% success Use Safe Args to generate type-safe navigation actions. Add the Safe Args plugin to build.gradle and replace manual navigate calls with generated Directions classes.
    Use Safe Args to generate type-safe navigation actions. Add the Safe Args plugin to build.gradle and replace manual navigate calls with generated Directions classes.
  3. 80% success Verify that the NavController is initialized with the correct NavGraph by calling navController.setGraph(R.navigation.nav_graph) before navigating.
    Verify that the NavController is initialized with the correct NavGraph by calling navController.setGraph(R.navigation.nav_graph) before navigating.

中文步骤

  1. 确保目的地 ID 在正确的 NavGraph 文件中声明(例如 nav_graph.xml),并且操作的 destination 属性完全匹配。示例:<fragment android:id="@+id/destHome" android:name="com.example.HomeFragment" />
  2. 使用 Safe Args 生成类型安全的导航操作。在 build.gradle 中添加 Safe Args 插件,并用生成的 Directions 类替换手动 navigate 调用。
  3. 通过调用 navController.setGraph(R.navigation.nav_graph) 在导航前验证 NavController 已使用正确的 NavGraph 初始化。

Dead Ends

Common approaches that don't work:

  1. 75% fail

    The destination ID might be misspelled or not present in the current NavGraph, causing the same error.

  2. 80% fail

    Each NavController uses a specific NavGraph; adding to the wrong file doesn't resolve the reference.

  3. 90% fail

    The error is a code configuration issue, not a cached state issue.