Another similar question也有类似的问题。但这并不能解决我的问题。
在构建我的项目之后,我得到了这样的错误:"@Composable invocations仅能从@composable function的上下文中发生“,尽管预览运行良好。
这是我的代码:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApp {
SplashUI()
}
}
}
@Composable
fun SplashUI() {
Image(painterResource(R.drawable.logo_voodlee),"content description")
}
@Composable
fun MyApp(content: @Composable () -> Unit) {
MaterialTheme {
Surface(color = Color.Yellow) {
content()
}
}
}
@Preview("MyScreen preview")
@Composable
fun DefaultPreview() {
MyApp {
SplashUI()
}
}已经被困在这里好几个小时了。请帮我解决这个问题!
发布于 2021-04-05 15:07:12
找到了解决方案。我导入了错误的setContent,并且错过了添加"androidx.activity:activity-compose:1.3.0-alpha05"添加它的依赖项,然后导入了正确的setContent,即androidx.activity.compose.setContent -这解决了问题。
https://stackoverflow.com/questions/66922326
复制相似问题