1

我有一个看起来像这样的 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
            )
        }
    }
}

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

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

产品完美显示,但当我单击产品时,应用程序因以下错误而崩溃:

java.lang.IllegalArgumentException:与请求匹配的导航目的地 NavDeepLinkRequest{ uri=android-app://androidx.navigation/Product Details } 在导航图中找不到 NavGraph(0x0) startDestination={Destination(0xb543811) route=Products}

任何人都可以帮忙吗?

PS我也跟着这个答案,但没有运气:(

4

1 回答 1

1

这是我的样本

@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")
          }
        }
     }
}
于 2022-01-12T13:03:29.743 回答