下面是我的代码,而构建代码我得到这个错误
调用@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)```
因为每个可组合函数都应该从另一个可组合函数调用。请参阅此解决方案,以便您理解并希望对您有用,
Jetpack compose AppBarIcon抱怨调用@Composable函数的函数必须用@Composable"标记