-1

There is a similar question Another similar question. But it doesn't solve my problem.

I get this error "@Composable invocations can only happen from the context of a @composable function" after building my project, even though the preview is working fine.

This is my code:

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

Been stuck at this for hours. Please help me fix this!!!

Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54

1 Answers1

1

Found the solution. I had imported the wrong setContent, and had missed adding the dependency "androidx.activity:activity-compose:1.3.0-alpha05" Added it, and then imported the right setContent, i.e androidx.activity.compose.setContent - this solved the issue.

Sparsh Dutta
  • 2,450
  • 4
  • 27
  • 54