以下是我在构建代码时遇到此错误的代码
调用@Composable 函数的函数必须使用@Composable 注解进行标记
@Composable 调用只能在 @Composable 函数的上下文中发生
@Composable 调用只能在 @Composable 函数的上下文中发生
显示错误
@Composable
public fun MessageCard(msg: Message) {
Text(text = msg.author)
Text(text = msg.body)
}
完整代码
class LayoutList : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MessageCard(Message("Android", "Jetpack Compose"))
}
}
}
@Composable
public fun MessageCard(msg: Message) {
Text(text = msg.author)
Text(text = msg.body)
}
@Preview
@Composable
fun PreviewMessageCard() {
MessageCard(
msg = Message("Colleague", "Hey, take a look at Jetpack Compose, it's great!")
)
}
data class Message(val author: String, val body: String)```