0

以下是我在构建代码时遇到此错误的代码

调用@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)```




4

1 回答 1

0

因为每个可组合函数都应该从另一个可组合函数调用。请参阅此解决方案以获取您的理解,希望对您有用,

Jetpack compose AppBarIcon 抱怨“调用@Composable 函数的函数必须用@Composable 标记”

于 2021-08-01T18:18:32.863 回答