无法启动活动ComponentInfo {className} java.lang.IllegalStateException:找不到颜色! (Android JetPack撰写)

时间:2020-01-15 09:02:43

标签: android kotlin android-jetpack-compose

我是Android Jetpack Compose的新手,试图创建带有Column的按钮列表,但应用程序因抛出错误而崩溃,

无法开始活动 ComponentInfo {com.sample.composeUI / com.sample.composeUI.ui.homeScreen.HomeScreenActivity}: java.lang.IllegalStateException:找不到颜色!

代码:

@Composable
fun homeScreenCompose() {

    Column(
        crossAxisAlignment = CrossAxisAlignment.Center,
        mainAxisAlignment = MainAxisAlignment.Center,
        modifier = Spacing(16.dp)
    ) {
        Button(
            text = "ListView", onClick = {
            }, style = ContainedButtonStyle(
                color = Color.White,
                shape = RectangleShape,
                rippleColor = Color.DarkGray,
                elevation = Dp(4f)
            ))
    }
}

找不到导致此问题的路由原因,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

Android内部使用材质设计来为视图提供颜色和错别字。因此,您需要将函数包装在MaterialTheme composable function中。

@Composable
fun homeScreenCompose() {
    MaterialTheme {
        Column(
            crossAxisAlignment = CrossAxisAlignment.Center,
            mainAxisAlignment = MainAxisAlignment.Center,
            modifier = Spacing(16.dp)
        ) {
            Button(
                text = "ListView", onClick = {
                }, style = ContainedButtonStyle(
                    color = Color.White,
                    shape = RectangleShape,
                    rippleColor = Color.DarkGray,
                    elevation = Dp(4f)
                )
            )
        }
    }
}

注意:当您使用撰写0.1.0-dev02版本时,会发生这种错误情况。 0.1.0-dev03版本没有必要。