不支持的操作异常:可串行化不支持默认值-Jetpack编写导航

UnsupportedOperationException: Serializables don#39;t support default values - Jetpack Compose Navigation(不支持的操作异常:可串行化不支持默认值-Jetpack编写导航)
本文介绍了不支持的操作异常:可串行化不支持默认值-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

";SerializableParcelable没有允许将字符串自动解析到定制类实例中的一致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编写导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)