我正在使用 Jetpack Compose Navigation 将 Health 实例传递给另一个可组合对象。下面的代码显示了我的健康类和我的目的地。
健康.kt:
data class Health(
val height: Int,
val weight: Int,
val age: Int,
val gender: Gender
) : Serializable
enum class Gender: Serializable { Male, Female }
NavDestination.kt
composable(
route = "result/{health}",
arguments = listOf(
navArgument("health") {
type =
NavType.SerializableType(Health::class.java)
}
)
) { backStackEntry ->
val health = (backStackEntry.arguments?.getSerializable("health") as? Health) ?: return@composable
ResultScreen(navActions = navActions, health = health)
}
NavActions.kt
val navigateToResultScreen = { health: Health ->
navController.navigate("result/{$health}")
}
但是,根据 logcat,我收到了此错误。任何帮助将不胜感激。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.octagon_technologies.bmicalculator, PID: 20788
java.lang.UnsupportedOperationException: Serializables don't support default values.
at androidx.navigation.NavType$SerializableType.parseValue(NavType.java:838)
at androidx.navigation.NavType$SerializableType.parseValue(NavType.java:791)
at androidx.navigation.NavType.parseAndPut(NavType.java:96)
at androidx.navigation.NavDeepLink.parseArgument(NavDeepLink.java:299)
at androidx.navigation.NavDeepLink.getMatchingArguments(NavDeepLink.java:260)
at androidx.navigation.NavDestination.matchDeepLink(NavDestination.java:474)
at androidx.navigation.NavGraph.matchDeepLink(NavGraph.java:79)
at androidx.navigation.NavController.navigate(NavController.java:1034)
at androidx.navigation.NavController.navigate(NavController.java:1017)
at androidx.navigation.compose.NavHostControllerKt.navigate(NavHostController.kt:107)
at androidx.navigation.compose.NavHostControllerKt.navigate$default(NavHostController.kt:106)
at com.octagon_technologies.bmicalculator.ui.navigation.NavActions$navigateToResultScreen$1.invoke(NavActions.kt:9)
at com.octagon_technologies.bmicalculator.ui.navigation.NavActions$navigateToResultScreen$1.invoke(NavActions.kt:8)
at com.octagon_technologies.bmicalculator.ui.screens.home.HomeScreenKt$HomeScreen$1$6.invoke(HomeScreen.kt:122)
at com.octagon_technologies.bmicalculator.ui.screens.home.HomeScreenKt$HomeScreen$1$6.invoke(HomeScreen.kt:120)
at com.octagon_technologies.bmicalculator.ui.components.home.CalculateBtnKt$CalculateButton$1$1$1.invoke(CalculateBtn.kt:29)
at com.octagon_technologies.bmicalculator.ui.components.home.CalculateBtnKt$CalculateButton$1$1$1.invoke(CalculateBtn.kt:29)
答案 0 :(得分:1)
为什么这不起作用:https://issuetracker.google.com/issues/148523779
“Serializable
和 Parcelable
没有一致的 API 表面,允许将字符串自动解析到您的自定义类实例中。这就是 parseValue
抛出 UnsupportedOperationException
的原因。”
预计你只会在这个深层链接中传递 id,而不是完整的对象
解决方法:
composable(
route = "result",
arguments = listOf(
navArgument("health") {
type =
NavType.SerializableType(Health::class.java)
}
)
) {
val health = (navController.previousBackStackEntry.arguments?.getSerializable("health") as? Health) ?: return@composable
ResultScreen(navActions = navActions, health = health)
}
像这样导航:
navController.currentBackStackEntry
?.arguments?.putSerializable("health", health)
navController.navigate("result")