IllegalArgumentException:此 NavController 未知导航目标 xxx [英] IllegalArgumentException: navigation destination xxx is unknown to this NavController

本文介绍了IllegalArgumentException:此 NavController 未知导航目标 xxx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从一个 Fragment 导航到另一个时,我遇到了新的 Android 导航架构组件的问题,我收到了这个奇怪的错误:

I am having an issue with the new Android Navigation Architecture component when I try to navigate from one Fragment to another, I get this weird error:

java.lang.IllegalArgumentException: navigation destination XXX
is unknown to this NavController

除了这个特定的导航之外,其他所有导航都可以正常工作.

Every other navigation works fine except this particular one.

我使用 Fragment 的 findNavController() 函数来访问 NavController.

I use findNavController() function of Fragment to get access to the NavController.

任何帮助将不胜感激.

推荐答案

在我的例子中,如果用户非常快速地点击同一个视图两次,就会发生这种崩溃.所以你需要实现某种逻辑来防止多次快速点击......这很烦人,但似乎是必要的.

In my case, if the user clicks the same view twice very very quickly, this crash will occur. So you need to implement some sort of logic to prevent multiple quick clicks... Which is very annoying, but it appears to be necessary.

您可以在此处阅读有关防止这种情况的更多信息:Android 防止双重点击按钮

You can read up more on preventing this here: Android Preventing Double Click On A Button

编辑 2019 年 3 月 19 日:为了进一步澄清,此崩溃不能仅通过非常快速地单击同一视图两次"来完全重现.或者,您可以只用两根手指同时单击两个(或更多)视图,其中每个视图都有自己的导航供它们执行.当您有一个项目列表时,这特别很容易做到.以上关于多次点击预防的信息将处理这种情况.

Edit 3/19/2019: Just to clarify a bit further, this crash is not exclusively reproducible by just "clicking the same view twice very very quickly". Alternatively, you can just use two fingers and click two (or more) views at the same time, where each view has their own navigation that they would perform. This is especially easy to do when you have a list of items. The above info on multiple click prevention will handle this case.

编辑 2020 年 4 月 16 日:以防万一您对阅读上面的 Stack Overflow 帖子不是很感兴趣,我将包括我自己的(Kotlin)解决方案,我一直在长期使用.

Edit 4/16/2020: Just in case you're not terribly interested in reading through that Stack Overflow post above, I'm including my own (Kotlin) solution that I've been using for a long time now.

class OnSingleClickListener : View.OnClickListener {

    private val onClickListener: View.OnClickListener

    constructor(listener: View.OnClickListener) {
        onClickListener = listener
    }

    constructor(listener: (View) -> Unit) {
        onClickListener = View.OnClickListener { listener.invoke(it) }
    }

    override fun onClick(v: View) {
        val currentTimeMillis = System.currentTimeMillis()

        if (currentTimeMillis >= previousClickTimeMillis + DELAY_MILLIS) {
            previousClickTimeMillis = currentTimeMillis
            onClickListener.onClick(v)
        }
    }

    companion object {
        // Tweak this value as you see fit. In my personal testing this
        // seems to be good, but you may want to try on some different
        // devices and make sure you can't produce any crashes.
        private const val DELAY_MILLIS = 200L

        private var previousClickTimeMillis = 0L
    }

}

ViewExt.kt

fun View.setOnSingleClickListener(l: View.OnClickListener) {
    setOnClickListener(OnSingleClickListener(l))
}

fun View.setOnSingleClickListener(l: (View) -> Unit) {
    setOnClickListener(OnSingleClickListener(l))
}

HomeFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    settingsButton.setOnSingleClickListener {
        // navigation call here
    }
}

这篇关于IllegalArgumentException:此 NavController 未知导航目标 xxx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆