android runtime_error ai_generated true

IllegalArgumentException:在 NavController 中未找到导航目的地 com.example:id/destHome

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

ID: android/navigation-destination-not-found

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2023-05-10首次发现

版本兼容性

版本状态引入弃用备注
AndroidX Navigation 2.5.3 active
AndroidX Navigation 2.6.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 初始化。

无效尝试

常见但无效的做法:

  1. 75% 失败

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

  2. 80% 失败

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

  3. 90% 失败

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