我使用的是nav控制器1.0.0alpha05,它运行良好,但是当我在活动结果之后执行导航操作时,我正在为这个可怕的错误而苦苦挣扎。
我有一个单一的活动/多个片段结构,特别是一个包含项目列表的片段,另一个具有添加新项目的形式。
当我添加另一张没有任何图片的图片时,该图片正在运行,并返回带有项目列表的上一张图片,但是当我拍摄一些照片时,导航期间会出现异常。
原因:java.lang.IllegalArgumentException:该NavController未知导航目标XX
包含操作的表单片段的导航图:
button1.setBackgroundImage(.image(with: .red), for: .normal)
带有安全参数的操作调用代码
<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>
感谢您的时间
答案 0 :(得分:3)
好吧,我设法找到了一个荒谬的解决方案...
假设您使用的是从NavHostFragment
扩展的主机导航片段,请向其添加以下(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
答案 1 :(得分:2)
我找到了一种解决方法,但显然我不能认为这是一种解决方案:
我认为,当恢复片段实例状态时,与该片段相关联的nav_graph动作的链接就以某种方式丢失或某些东西……但是我可能是错的
在这种情况下,可以直接使用您要导航到的片段的ID,而不是通过安全的arg或其ID指向动作本身。
在这种情况下,如果要传递参数,则必须使用捆绑包中的旧方法。
Bundle args = new Bundle();
args.putString(ID_ARG, arg);
Navigation.findNavController(getView()).navigate(R.id.fragmentId, args);
答案 2 :(得分:0)
在我的情况下,这是因为我使用fragmentManager?.popBackStack()
向后导航,而不是正确导航,如下所示:
Navigation.findNavController(activity!!, R.id.fragment_container).navigate(
Navigation.findNavController(activity!!, R.id.fragment_container).graph.startDestination)
答案 3 :(得分:0)
对于我来说,我通过替换-
解决了问题 <action
android:id="@+id/actionFormToList"
app:destination="@id/idListFragment" />
使用
<action
android:id="@+id/actionFormToList"
app:popUpToInclusive="true" /* If true then also remove the destination from stack while popup */
app:popUpTo="@id/idFormFragment " /*The fragment where to land again from destination*/
app:destination="@id/idListFragment" />