kotlin 无法从JetpackCompose中的另一个NavGraph导航到屏幕

vmpqdwk3  于 2023-03-19  发布在  Kotlin
关注(0)|答案(1)|浏览(216)

我有应用程序,我有2个导航图。第一个是RootNavGraph

@Composable
fun RootNavGraph(navController: NavHostController) {
    NavHost(navController, startDestination = RootGraphItem.Splash.route) {
        composable(route = RootGraphItem.Splash.route) {
            SplashScreen(navController = navController)
        }
        composable(route = RootGraphItem.Login.route) {
            LoginScreen(navController = navController)
        }
        composable(route = RootGraphItem.Register.route) {
            RegisterScreen(navController = navController)
        }
        composable(route = RootGraphItem.Main.route) {
            MainScreen()
        }
        composable(route = RootGraphItem.BasicInfo.route) {
            BasicInfoScreen(navController = navController)
        }
        composable(route = RootGraphItem.GenderIdentity.route) {
            GenderIdentityScreen(navController = navController)
        }
        composable(route = RootGraphItem.SexualOrientation.route) {
            SexualOrientationScreen(navController = navController)
        }
        composable(route = RootGraphItem.Pronouns.route) {
            PronounsScreen(navController = navController)
        }
        composable(route = RootGraphItem.Interests.route) {
            InterestsScreen(navController = navController)
        }
        composable(route = RootGraphItem.Description.route) {
            DescriptionScreen(navController = navController)
        }
        composable(route = RootGraphItem.Photos.route) {
            PhotosScreen(navController = navController)
        }
        composable(route = RootGraphItem.SearchingPreferences.route) {
            SearchingPreferencesScreen(navController = navController)
        }
        composable(route = RootGraphItem.AgeHeightLocation.route) {
            AgeHeightLocationScreen(navController = navController)
        }
        composable(route = RootGraphItem.WhyYouHere.route) {
            WhyYouHereScreen(navController = navController)
        }
        composable(route = RootGraphItem.Start.route) {
            StartScreen(navController = navController)
        }
        composable(route = RootGraphItem.Location.route) {
            LocationScreen(navController = navController)
        }
        composable(route = RootGraphItem.UniversityWork.route) {
            UniversityWorkScreen(navController = navController)
        }
    }
}

第二个是BottomNavGraph(用于底部导航栏):

@Composable
fun BottomNavGraph(navController: NavHostController) {
    NavHost(navController, startDestination = BottomNavItem.Home.screen_route) {
        composable(route = BottomNavItem.Home.screen_route){
            HomeScreen(navController)
        }
        composable(route = BottomNavItem.Chat.screen_route){
            LastChatsScreen()
        }
        composable(route = BottomNavItem.Profile.screen_route){
            ProfileScreen(navController)
        }
    }
}

我在配置文件屏幕中有一个注销图标,来自底部导航栏

@Composable
fun ProfileScreen(navController: NavHostController) {
    val context = LocalContext.current
    val loggedInViewModel: LoggedInViewModel = viewModel()
    var loggedOut = loggedInViewModel.getLoggedOutLiveData().observeAsState()
    if(loggedOut.value != null && loggedOut.value == true) {
        navController.navigate(RootGraphItem.Login.route) <- error occurs here
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
            .verticalScroll(rememberScrollState())
    ) {
        Row(
            modifier = Modifier
                .padding(horizontal = 16.dp, vertical = 32.dp)
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "Your profile",
                style = TextStyle(fontSize = 32.sp, fontWeight = FontWeight.W400)
            )
                Icon(
                    modifier = Modifier
                        .size(32.dp)
                        .clickable {
                            loggedInViewModel.logOut()
                        },
                    painter = painterResource(id = R.drawable.icon_logout),
                    contentDescription = null
                )
        }
        ...
}

单击注销图标后,应用程序应注销用户并导航至登录屏幕,但它返回错误:

java.lang.参数非法异常:在导航图中找不到与请求NavDeepLink匹配的导航目的地请求{ uri=android-app://androidx.navigation/login_screen } NavGraph(0x 0)startDestination={目的地(0x 442 b361 f)route=主页屏幕}

导航出了什么问题?怎么处理?

bihw5rsg

bihw5rsg1#

您需要在startDestination或App类中向RootNavGraph发送一个navController,在传递它之前,您应该像下面这样分配它;

val navController = rememberNavController()
RootNavGraph(navController)

你可以这样处理。

相关问题