本文介绍了不支持的操作异常:可串行化不支持默认值-Jetpack编写导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Jetpack Compose导航将Health实例传递给另一个Composable。下面的代码显示了我的健康类和我的目标。
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}")
}
但是,根据日志目录,我收到了这个错误。如有任何帮助,我们将不胜感激。
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)
推荐答案
此操作失败的原因: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")
这篇关于不支持的操作异常:可串行化不支持默认值-Jetpack编写导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!