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

Unable to start activity ComponentInfo {className} java.lang.IllegalStateException: No colors found! (Android JetPack Compose)

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

Unable to start activity ComponentInfo{com.sample.composeUI/com.sample.composeUI.ui.homeScreen.HomeScreenActivity}: java.lang.IllegalStateException: No colors found!

代码:

@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)
            ))
    }
}

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

Android compose 在内部使用 Material 设计为您的视图提供颜色和排版。所以你需要把你的函数包装在 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)
                )
            )
        }
    }
}

注意: 当您使用 compose 0.1.0-dev02 版本时会出现此错误情况。 0.1.0-dev03 版本不需要。