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

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

问题描述

当我尝试从一个片段导航到另一个片段时,我遇到了新的Android导航体系结构组件的问题,我得到了这个奇怪的错误:

I am having 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

编辑3/19/2019 :只需进一步说明一下,此崩溃并非只能通过非常非常快地单击相同的视图两次"来重现.另外,您也可以只用两个手指同时单击两个(或多个)视图,每个视图都有它们自己将执行的导航.当您有项目列表时,尤其是容易做到.上面有关多次点击防护的信息将解决这种情况.

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.

编辑4/16/2020 :以防万一,如果您对阅读上面的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天全站免登陆