kotlin 在导航图NavGraph中找不到与请求NavDeepLinkRequest匹配的导航目的地

nfeuvbwi  于 2023-03-24  发布在  Kotlin
关注(0)|答案(4)|浏览(393)

我有一个NavGraph,看起来像这样:

@Composable
fun NavGraph (
    navController: NavHostController
) {
    NavHost(
        navController = navController,
        startDestination = "Products"
    ) {
        composable(
            route = "Products"
        ) {
            ProductsScreen(
                navController = navController
            )
        }
        composable(
            route = "Product Details",
            arguments = listOf(
                navArgument("product") {
                    type = NavType.SerializableType(Product::class.java)
                }
            )
        ) {
            val product = navController.previousBackStackEntry?.arguments?.getSerializable("product") as Product
            ProductDetailsScreen(
                navController = navController,
                product = product
            )
        }
    }
}

在ProductDetailsScreen中,我希望单击产品以进一步导航到详细信息屏幕,并传递Product对象:

LazyColumn {
    items(
        items = products
    ) { product ->
        ProductCard(
            product = product,
            onProductClick = {
                navController.currentBackStackEntry?.arguments?.putSerializable("product", product)
                navController.navigate("Product Details")
            }
        )
    }
}

产品显示完美,但当我点击产品时,应用程序崩溃并显示此错误:
java.lang.非法参数异常:在导航图NavGraph(0x 0)startDestination={Destination(0xb 543811)route=Products}中找不到与请求NavDeepLinkRequest{ uri=android-app://androidx.navigation/Product Details }匹配的导航目标
有人能帮忙吗?
我也关注了这个answer,但是没有运气

mcvgt66p

mcvgt66p1#

确保所有参数名和路由名都正确设置,避免使用空格,如果要断词,请使用下划线
我的错误
路由定义中的参数名称和在navArgument块中提供的参数名称应该相同

composable(
                        "pokemon_detail_screen/{dominantColor}/{pokemonName}", //Notice over here
                        arguments = listOf(
                            navArgument("dominantColor") {
                                type = NavType.IntType
                            },
                            navArgument("pokemon_name") { // Notice over here
                                type = NavType.StringType
                            }
                        )
                    )

我花了很长时间才弄明白所以要非常小心

63lcw9qa

63lcw9qa2#

这是我的样品

@ExperimentalPagerApi
@Composable
fun WallNavigation(nameFolder: String, paths: List<String>) {
   val navController = rememberNavController()
   val model: ImageViewModel = viewModel()
   model.setFolder(nameFolder)
   model.setFileNames(paths)

   NavHost(navController = navController, startDestination = Screen.MainScreen.route) {
    composable(Screen.MainScreen.route) {
        MainScreen(navController = navController, model)
    }

    composable(route = Screen.GalleryScreen.route + "/{position}", arguments = listOf(navArgument(name = "position") {
        type = NavType.IntType
        defaultValue = -1
        nullable = false
    })) { entity ->
        GalleryScreen(navController = navController, position = entity.arguments?.getInt("position") ?: -1, imageViewModel = model)
    }

    composable(route = Screen.CropScreen.route) {
        CropScreen(navController = navController, imageViewModel = model)
    }
  }
} 

sealed class Screen(val route : String) {
    object MainScreen : Screen("main_screen")
    object GalleryScreen : Screen("gallery_screen")
    object CropScreen : Screen("crop_screen")

    fun withArgs(vararg args: String):String {
       return buildString {
          append(route)
          args.forEach { arg ->
              append("/$arg")
          }
        }
     }
}
jecbmhm3

jecbmhm33#

当我使用json作为字符串将class传递给我的导航时,我遇到了这个错误,下面是我如何实现的:

composable(
            "details/{MyObject}",
            arguments = listOf(navArgument("MyObject") {
                type = NavType.StringType
            })
        ) {
            it.arguments?.getString("MyObject")?.let { jsonString ->
                var issue = MyObject()
                if (!jsonString.isNullOrEmpty())
                    issue = jsonString.fromJson(MyObject::class.java)
                DetailsView(navController = navController, detailsViewModel, myObject = MyObject)
            }
        }

我怎么称呼它:

val gson = Gson()
var myObjectString = gson.toJson(myObject, MyObject::class.java)
navController.navigate("details/$myObjectString")

有时我得到Navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph错误,有时我没有得到它。这取决于我点击的对象。
我想这是因为当我从API得到答案时,我的一些数据格式不好,这意味着当我把它转换成json时,我可能会有一些不需要的字符。为了避免这种情况,我用utf8编码json,如下所示:

var encode = URLEncoder.encode(myObjectString,StandardCharsets.UTF_8.toString())
navController.navigate("details/$encode")

然后我没有得到导航错误再次,我希望这可以帮助那些试图使用jetpack编写导航与json发送对象的人

cl25kdpy

cl25kdpy4#

This answer很好地解释了如何通过组合导航传递可打包参数。
您上面描述的错误消息the navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph也可能出现在您想要移交可选参数并且忘记将其设置为可空时。

arguments: List<NamedNavArgument> = listOf(
            navArgument("product") {
                type = Product.NavigationType //check out the linked answer to know how to write the Product.NavigationType
                nullable = true
            }
        )

相关问题