我有一个片段a、B、C.从a->B、 但是从B->C它崩溃了.

这是我的导航

enter image description here

这是我的导航码

 categoryProductItemListAdapter.setOnItemClickListener {
        val action = CategoryProductItemsDirections.actionCategoryProductItems2ToProductItem(null, it)
        navController = Navigation.findNavController(requireView())
        navController?.navigateUp()
        navController?.navigate(action)
    }

Here is the XML code for the destination to productItem

<fragment
    android:id="@+id/categoryProductItems2"
    android:name="com.sample.store.main.dashboard.ui.ui.home.categoryitems.CategoryProductItems"
    android:label="CategoryProductItems"
    tools:layout="@layout/fragment_category_product_items">
    <argument
        android:name="category_global"
        app:argType="com.sample.store.data.globalmodels.response.categories.Category" />
    <action
        android:id="@+id/action_categoryProductItems2_to_productItem"
        app:destination="@id/productItem"
        app:enterAnim="@anim/enter_from_right"
        app:exitAnim="@anim/exit_to_right"
        app:popEnterAnim="@anim/fragment_open_enter"
        app:popExitAnim="@anim/fragment_fade_exit" />
</fragment>

And here is the error:

java.lang.IllegalArgumentException: Navigation action/destination com.sample.store.full:id/action_categoryProductItems2_to_productItem cannot be found from the current destination Destination(id/navigation_home) label=Home class=com.sample.store.main.dashboard.ui.ui.home.mainui.HomeFragment

I don't know what happened, but it seems that the navController is looking for the "navigation_home"

推荐答案

Firstly you should not pass requireView() when trying to retrieve your Nav controller - navController = Navigation.findNavController(requireView()). You should be passing the actual Navigation Host Fragment instance.

Secondly the issue is being caused because you are trying to call a Navigation path from B -> C, when on fragment A.

Your direction path is from B -> C

val action = CategoryProductItemsDirections.actionCategoryProductItems2ToProductItem(null, it)

但是你先向上导航,所以当你试图执行导航时,你实际上现在在片段A上:

navController?.navigateUp()
navController?.navigate(action)

Kotlin相关问答推荐

可以从背景图像中点击图标吗?

如何在Android应用判断上运行多个查询

哪个更好? Integer.valueOf(readLine()) 或 readLine()!!.toInt()

在 Kotlin 中实现并输入 Either

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

如何在 Android Jetpack Compose 中的画布上绘制一侧加厚的描边?

Kotlin:查找集合中最常见的元素

Kotlin - 当表达式返回函数类型

如何在 Kotlin 中为变量设置监听器

Kotlin:如何在活页夹中返回正在运行的服务实例?

如何在 Kotlin 中传递有界通配符类型参数?

在java代码中使用kotlin库

Kotlin:使用Gradle进行增量编译

kotlin RecyclerView分页

Jetpack Compose-居中文本

在Kotlin中使用@Service时引发异常

未解决的参考 dagger 2 + kotlin + android gradle

修改扩展函数中的this

Kotlin - 如何获取注释属性值

如何在 Kotlin 中定义新的运算符?