Android 可组合应用程序在尝试加载食物菜单列表中的图像时崩溃

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

祝所有阅读本文的人早安或晚安。

我目前正在学习 Android 开发,并且在课程的特定部分遇到了障碍。因为我是新人,所以我已经期待着笑声,当我打字想象反应时我已经笑了。 Reactions to my code

严格按照所有说明操作后,应用程序崩溃。我注意到,如果我删除或注释掉特定于调用“食物”菜单图像的代码行,该应用程序运行良好......但当我把该行放回原处时,它就无法运行。请参阅下面以粗体显示的代码片段:

@Composable
fun LowerPanel(navController: NavHostController, dishes: List<Dish> = listOf()) {
    Column {
        WeeklySpecialCard()
        LazyColumn {
            itemsIndexed(dishes) { _, dish ->
                MenuDish(navController, dish)
            }
        }
    }
}

@Composable
fun WeeklySpecialCard() {
    Card(
        modifier = Modifier
            .fillMaxWidth()
    ) {
        Text(
            text = stringResource(R.string.weekly_special),
            fontSize = 26.sp,
            fontWeight = FontWeight.Bold,
            color = LittleLemonColor.charcoal,
            modifier = Modifier
                .padding(8.dp)
        )
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MenuDish(navController: NavHostController? = null, dish: Dish) {
    Card(onClick = {
        Log.d("AAA", "Click ${dish.id}")
        navController?.navigate(DishDetails.route + "/${dish.id}")
    }) {
        Row {
            Modifier.fillMaxWidth()
            Modifier.padding(8.dp)
            Column {
                Text(text = dish.name,
                    color = LittleLemonColor.charcoal,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold)
                Text(text = dish.description,
                    fontWeight = FontWeight.Bold,
                    color = LittleLemonColor.green,
                    modifier = Modifier
                        .fillMaxWidth(.75f)
                        .padding(top = 5.dp, bottom = 5.dp))
                Text(text = "${dish.price}",
                    fontWeight = FontWeight.Bold,
                    color = LittleLemonColor.green)
            **}
            Image(painter = painterResource(id = dish.imageResource),
                    contentDescription = "",
                    modifier = Modifier.clip(RoundedCornerShape(1.dp)))**
        }
    }
    Divider(
        modifier = Modifier.padding(start = 8.dp, end = 8.dp),
        thickness = 1.dp,
        color = LittleLemonColor.yellow
    )
}

我还附上了 DishRepository.kt,其中包含每个项目的菜肴编码。我也仔细检查了图像,它们看起来稳定且兼容。

object DishRepository {
    val dishes = listOf(
        Dish(
            1,
            "Greek Salad",
            "The famous greek salad of crispy lettuce, peppers, olives, our Chicago.",
            12.99,
            R.drawable.greeksalad
        ),
        Dish(
            2,
            "Lemon Desert",
            "Traditional homemade Italian Lemon Ricotta Cake.",
            8.99,
            R.drawable.lemondessert
        ),
        Dish(
            3,
            "Bruschetta",
            "Our Bruschetta is made from grilled bread that has been smeared with garlic and seasoned with salt and olive oil.",
            4.99,
            R.drawable.bruschetta
        ),
        Dish(
            4,
            "Grilled Fish",
            "Fish marinated in fresh orange and lemon juice. Grilled with orange and lemon wedges.",
            19.99,
            R.drawable.grilledfish
        ),
        Dish(
            5,
            "Pasta",
            "Penne with fried aubergines, cherry tomatoes, tomato sauce, fresh chilli, garlic, basil & salted ricotta cheese.",
            8.99,
            R.drawable.pasta
        ),
        Dish(
            6,
            "Lasagne",
            "Oven-baked layers of pasta stuffed with Bolognese sauce, béchamel sauce, ham, Parmesan & mozzarella cheese .",
            7.99,
            R.drawable.lasagne
        )
    )

    fun getDish(id: Int) = dishes.firstOrNull { it.id == id }
}

data class Dish(
    val id: Int,
    val name: String,
    val description: String,
    val price: Double,
    @DrawableRes val imageResource: Int
)

这就是我应该得到的结果,app example。 当我删除 Image 元素时,我可以毫无错误地加载应用程序。但出于显而易见的原因,这些照片是菜单的一部分:-)。这个项目的大部分内容已经奠定了基础,我被指示插入提供的图像、修改文本、修改原色以及利用其他可组合元素。请让我知道我或这个项目的基础哪里出了问题...我很乐意用它作为我妻子的生意(不是餐厅)的个人项目的基础。

即使可以为我提供有关如何正确调试或调查 Image 元素导致应用程序崩溃的原因的指南,也会很有帮助。

再次感谢大家的时间和关注。

android image kotlin crash android-jetpack-compose
1个回答
0
投票

更新 了解 Logcat(实际上是 5 分钟前)后,我已将问题隔离到导致应用程序崩溃的 6 个图像中的 2 个。所有图像均为 JPG 文件。加载的文件大小范围在 126KB 到 6.3MB 之间。导致应用程序崩溃的两个文件分别是 12.7MB 和 3.8MB。此时,我将提交项目进行评分,并在代码中注释掉这两个图像,因为我收到了损坏的数据!我在这里努力! :-) 再次感谢@Daqs 教我 Logcat!

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