【问题标题】:@Composable invocations can only happen from the context of a @composable function@Composable 调用只能在 @composable 函数的上下文中发生
【发布时间】:2021-04-02 16:27:16
【问题描述】:

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

我在构建项目后收到此错误“@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()
            }
        }

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

【问题讨论】:

  • “我收到这个错误”——具体是在哪里收到这个错误的?
  • 您使用的是哪个版本的 compose?
  • @GabrieleMariotti,我使用的是 1.0.0-beta03
  • @CommonsWare,在构建输出中。

标签: android android-jetpack-compose


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多