这个问题在这里已经有了答案:
IllegalArgumentException: navigation destination xxx is unknown to this NavController
(39 个回答)
去年关闭。
我有一个使用导航组件构建的应用程序。我的图表中有一个字段,在单击工具栏上的过滤器图标时,它会向上滑动底部的工作表对话框 fragment 。但是,如果我非常快地双击工具栏过滤器图标或非常快地单击工具栏过滤器图标和任何其他与导航相关联的 View ,我的应用程序将崩溃并显示以下错误消息:
java.lang.IllegalArgumentException: navigation destination com.th3pl4gu3.locky:id/action_Fragment_Card_to_Fragment_View_Card is unknown to this NavController
下面是我的工具栏过滤器图标的代码示例。
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.Toolbar_Filter -> {
findNavController().navigate(CardFragmentDirections.actionFragmentCardToBottomSheetFragmentCardFilter())
true
}
else -> false
}
}
这是导航架构组件的正常行为吗?
如果没有,你能给我一个修复吗?
最佳答案
currentId
是 CardFragment
在 navGraph 中定义 id
fun Fragment.findNavControllerSafety(currentId: Int): NavController? {
try {
val controller = NavHostFragment.findNavController(this)
if (controller.currentDestination?.id != currentId) {
val name = controller.currentDestination?.let {
Utils.getApp().resources.getResourceName(it.id)
} ?: ""
LogLogger.i("Navigation currentDestination not match: $name")
return null
}
return controller
} catch (e: Exception) {
return null
}
}
关于Android:快速双击与导航组件相关的任何 View 会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61393629/