当我得到这么多参数时,错误发生了,我已经尝试了在StackOverflow中阅读的一些解决方案,但它们在我的代码中不起作用。我只是想问我该怎么处理这个错误。
代码:
sealed class NavigationScreen(val route: String) {
object HeroesPageScreen: NavigationScreen("hero_information_screen")
object HeroDetails: NavigationScreen("hero_details_screen")
fun withArgs(vararg args: String): String {
return buildString {
append(route)
args.forEach { arg ->
append("/$arg")
}
}
}
}
@ExperimentalFoundationApi
@Composable
fun NavigationHero() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination =
NavigationScreen.HeroesPageScreen.route) {
composable(route = NavigationScreen.HeroesPageScreen.route) {
HeroesPageScreen(navController = navController)
}
composable(route = NavigationScreen.HeroDetails.route + "/{item}",
arguments = listOf(
navArgument("item") {
type = NavType.StringType
nullable = true
})
){
navBackStackEntry ->
navBackStackEntry.arguments?.getString("item")?.let { json ->
val item = Gson().fromJson(json, HeroInfoData::class.java)
HeroDetails(data = item)
}
}
}
}
@Composable
fun HeroCardItem(data: HeroInfoData, navController: NavController) {
Card(
modifier = Modifier
.clickable {
val itemId = Gson().toJson(data)
navController.navigate(NavigationScreen.HeroDetails.route + "/$itemId")
}
.padding(5.dp)
.fillMaxSize(),
elevation = 5.dp,
shape = RoundedCornerShape(5.dp)
) {...}
}
当我运行我的代码时,会出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dota2dict, PID: 7361
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/hero_details_screen/{"attackType":"MELEE","attackTypeImage":1,"complexityImage":1,"heroDescription":"SHIELDS HIS ALLIES OR HIMSELF FROM ATTACKS","heroHistory":"Able to transform enemy attacks into self-healing, Abaddon can survive almost any assault. Shielding allies and launching his double-edged coil at a friend or foe, he is always ready to ride into the thick of battle.","heroName":"ABADDONn","heroType":"Strength","imageId":1,"roleCarry":25,"roleDisabler":0,"roleDurable":75,"roleEscape":0,"roleInitiator":0,"roleJungler":0,"roleNuker":0,"rolePusher":0,"roleSupport":75,"statArmor":"2.8","statAttackDamage":"50-60","statAttackRange":"150","statAttackTime":"1.7","statMovementSpeed":"325","statVision":"1800/800","typeImage":1} } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x19e6c117) route=hero_information_screen}
如果你的json包含url,它可能会出现,但我在你的中没有看到url。
试试这个
composable(route = NavigationScreen.HeroDetails.route + "?item={item}"
navController.navigate(NavigationScreen.HeroDetails.route + "?item=$itemId")