我是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)
))
}
}
找不到导致此问题的路由原因,我们将不胜感激。
答案 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
版本没有必要。