@Composable 调用只能在@composable 函数的上下文中发生

@Composable invocations can only happen from the context of a @composable function

有一个类似的问题。 但这并没有解决我的问题。

我在构建项目后收到此错误“@Composable 调用只能在@composable 函数的上下文中发生”,即使预览工作正常。

这是我的代码:

    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()
            }
        }

被困在这个问题上好几个小时了。 请帮我解决这个问题!!!

找到解决方案。我导入了错误的 setContent,并且错过了添加依赖项 "androidx.activity:activity-compose:1.3.0-alpha05" 添加它,然后导入正确的 setContent,即 androidx.activity.compose.setContent - 这解决了问题。