以下是我在构建代码时出现此错误的代码
调用@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)```
答案 0 :(得分:0)
因为每个可组合函数都应该从另一个可组合函数中调用。 请参阅此解决方案以获得您的理解,希望对您有用,