Android:快速双击与导航组件相关的任何 View 会使应用程序崩溃

标签 android kotlin android-architecture-navigation android-jetpack-navigation android-bottomsheetdialog

这个问题在这里已经有了答案:





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

这是导航架构组件的正常行为吗?

如果没有,你能给我一个修复吗?

最佳答案

currentIdCardFragment在 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/

相关文章:

android - Android 上的蓝牙 : startDiscovery doesn't work and I have bluetooth enabled

可选的 Kotlin 多平台支持

android - 如何在 Kotlin 中注入(inject)原始变量?

android - 非法状态异常 : Link does not have a NavController set

android - 使用 Android 导航组件的深层链接打开不正确的屏幕

android - 安全参数 : use list of parcelables

Android eglSwapBuffers - 手动控制? (安卓)

android - 具有网格布局管理器的 Recyclerview 的相等填充

Android Studio,如何设置共享库项目

android - runInTransaction block 内的挂起方法