startDestination 出现错误会在具有类型安全性的 Wear Navigation Compose 中出现错误

问题描述 投票:0回答:1

如何在具有类型安全版本的 Wear compose 中使用导航库。 SwipeDismissableNavHost 的 startDestination 属性不接受像 NavHost 这样的 Any 值。

用于佩戴导航撰写的依赖项:

androidx.wear.compose:compose-navigation:1.5.0-alpha04

请找到以下代码和错误:

java.lang.IllegalArgumentException:导航目标主页不是此 NavGraph 的直接子级

代码:

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()

super.onCreate(savedInstanceState)

setTheme(android.R.style.Theme_Black)

setContent {
    WearAppIMSTheme {
        Scaffold(timeText = {
            TimeText(
                timeTextStyle = TimeTextDefaults.timeTextStyle(
                    color = MaterialTheme.colors.onBackground
                )
            )
        },
            content = {
                DemoApp(modifier = Modifier)
            })
    }
}
}
}

@Composable
fun DemoApp(modifier: Modifier) {

Box(
modifier = modifier
    .background(MaterialTheme.colors.background)
) {
val navController = rememberSwipeDismissableNavController()


/**
 * NavHost Builds a navGraph to handle navigation, set the start destination to Home and
 * provide the navController which will control the navigation.
 */


SwipeDismissableNavHost(
    navController = navController,
    startDestination = ("home") // Getting Error: IllegalArgumentException: navigation destination home is not a direct child of this NavGraph
) {

    composable<Routes.Home> {
        Home(navigateToDetails = { aptDetails ->
            navController.navigate(Routes.AppointmentDetail(aptDetails))
        }, navigateToMySchedule = {
            navController.navigate(Routes.MySchedule)
        }, navigateToWaitingApts = {
            navController.navigate(Routes.WaitingForMe)
        })
    }

    composable<Routes.WaitingForMe> {
        WaitingForMe(onBackPress = {
            navController.popBackStack()
        })
    }

    composable<Routes.AppointmentDetail>(
        typeMap = mapOf(typeOf<ApptList>() to NavType.fromCustom<ApptList>())
    ) {
        val apptList = rememberSaveable {
            it.toRoute<Routes.AppointmentDetail>().apptDetail
        }
        AppointmentDetail(apptList, onBackPress = {
            navController.popBackStack()
        }, modifier)
    }

    composable<Routes.MySchedule> {

        MySchedule(onBackPress = {
            navController.popBackStack()
        }, modifier = Modifier,
            navigateToDetails = {
                navController.navigate(Routes.AppointmentDetail(it))
            })
    }
}

}

}
android-jetpack-compose android-jetpack-navigation android-wear-2.0
1个回答
0
投票

使用类型安全导航,您应该将 startDestination 设置为

Routes.Home
startDestination = Routes.Home::class
(它是一个
object
),或者
Routes.Home()
(如果它是一个类)。

© www.soinside.com 2019 - 2024. All rights reserved.