应用程序未启动,Jetpack Compose 中的导航出现问题

App is not launching, problem with Navigation in Jetpack Compose

提问人:Aleksandr Tsoy 提问时间:11/12/2023 最后编辑:Aleksandr Tsoy 更新时间:11/12/2023 访问量:50

问:

我在单独的文件中添加了 BottomNavbar、BottomnavGraph,它正在工作,但现在我想通过单击使一个框可单击以移动到另一个类: 主屏幕页面:

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen() {
    val navController = rememberNavController()
    BottomNavGraph(navController = navController)


        Page1(navController = navController)
    
}


@Composable
fun Page1(navController: NavHostController) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .verticalScroll(rememberScrollState())
    ) {
        Logo()
        Search()
        SaleBox()
        Catalog(navController = navController)

    }
}

...

@Composable
fun Catalog(navController: NavHostController) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(100.dp),
        contentAlignment = Alignment.Center,

        ) {
        Text(
            text = "Catalog",
            fontSize = 30.sp,
            fontWeight = FontWeight.Bold
        )
    }

    Column {


        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceAround

        ) {

            Box {
                Image(
                    painter = painterResource(id = R.drawable.frame), contentDescription = "frame",
                    modifier = Modifier
                        .width(170.dp)
                        .height(200.dp)
                        .clickable {
                            navController.navigate("Info")
                        }
                )

底部导航图:

package com.example.myapplication

import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable



@Composable
fun BottomNavGraph(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = BottomNavItem.Home.route
    ) {
        composable(route = BottomNavItem.Home.route) {
            MainScreen()
        }
        composable(route = BottomNavItem.Wishlist.route) {
            Wish()
        }
        composable(route = BottomNavItem.Cart.route) {
            Cart()
        }
        composable(route = GetInfo.Info.route) {
            Info()
        }

    }
}

BottomNavItem:

package com.example.myapplication

sealed class BottomNavItem(
    var title: String,
    var route: String,
    var icon: Int
) {
    object Home :
        BottomNavItem(
            "Home",
            "Home",
            R.drawable.home
        )

    object Cart :
        BottomNavItem(
            "Cart",
            "Cart",
            R.drawable.shoppingcart
        )

    object Wishlist :
        BottomNavItem(
            "Wishlist",
            "Wishlist",
            R.drawable.heart
        )

    object Sell :
        BottomNavItem(
            "Sell",
            "Sell",
            R.drawable.usdollarcircled
        )


}
sealed class GetInfo(
    val route: String = "Info"
) {
    object Info : GetInfo()
}

添加后,该应用程序甚至没有启动 Logcat 太长,无法捕捉到问题的开始。请帮帮我( BottomNavGraph(navController = navController)

我希望通过单击框移动到另一个班级

Android Kotlin android-jetpack 喷气背包

评论


答: 暂无答案