gpt4 book ai didi

android - Activity 结果后此 NavController 未知的导航目的地

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:57:04 29 4
gpt4 key购买 nike

我正在使用导航 Controller 1.0.0alpha05,它工作得很好,但是当我在 Activity 结果后执行导航操作时,我正在为这个可怕的错误而苦苦挣扎。

我有一个单一的 Activity/多个 fragment 结构,特别是一个带有项目列表的 fragment 和另一个带有用于添加新项目的表单的 fragment 。

当我添加另一张没有任何图片的照片时,它正在工作并返回到带有项目列表的前一张,但是当我拍摄一些照片时,我在导航过程中遇到异常。

Caused by: java.lang.IllegalArgumentException: navigation destination XX is unknown to this NavController

Error log

包含 Action 的表单 fragment 的导航图:

<fragment
android:id="@+id/idFormFragment"
android:name="FormFragment"
android:label="FormFragment"
tools:layout="@layout/form_fragment">
<argument
android:name="idClient"
android:defaultValue="-1"
app:argType="integer" />
<argument
android:name="idServer"
app:argType="string" />
<action
android:id="@+id/actionFormToList"
app:destination="@id/idListFragment" />
</fragment>

带有安全参数的 Action 调用代码

FormFragmentDirections.ActionFormToList action = new FormFragmentDirections.ActionFormToList(sample.getIdJob());
Navigation.findNavController(getView()).navigate(action);

谢谢你的时间

最佳答案

好吧,我设法找到了一个荒谬的解决方案......

假设您正在使用从 NavHostFragment 扩展的主机导航 fragment ,将此 (Kotlin) 代码添加到其中:

/*
* begin DUMB Navigation Component hack
*
* This fixes an IllegalArgumentException that can sometimes be thrown from within the
* Navigation Architecture Component when you try to navigate after the Fragment has had its
* state restored. It occurs because the navController's currentDestination field is null,
* which stores where we currently are in the navigation graph. Because it's null, the
* Navigation Component can't figure out our current position in relation to where we're
* trying to navigate to, causing the exception to be thrown.
*
* This fix gives the navController a little nudge by gently setting it to where we currently
* are in the navigation graph.
*
* This fix is verified as both working AND necessary as of Navigation Components version
* 1.0.0-alpha07.
*
* There's a tiny bit more information at this thread, but it's pretty limited:
* https://stackoverflow.com/questions/52101617/navigation-destination-unknown-to-this-navcontroller-after-an-activity-result
*/
private var checkCurrentDestination = false

override fun onStart() {
super.onStart()

if (checkCurrentDestination && navController.currentDestination == null) {
navController.navigate(navController.graph.startDestination)
}

checkCurrentDestination = false
}

override fun onStop() {
super.onStop()
checkCurrentDestination = true
}
/*
* end DUMB Navigation Component hack
*/

在 SEO 的努力中,堆栈跟踪将如下所示:

Caused by: java.lang.IllegalArgumentException: navigation destination XX is unknown to this NavController

关于android - Activity 结果后此 NavController 未知的导航目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52101617/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com