Search code examples
androidandroid-jetpack-compose

Unresolved reference on setContent() while migrating to Compose


setContent() is giving me an unresolved reference when using the code in the tutorial. It's strange because the compiler is recognizing setContent() (or it wouldn't be giving the helper error). I'm unsure why this is not building.

Other similar questions on Stack Overflow didn't answer this one.

import androidx.activity.compose.setContent

class MyClass {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent { // Unresolved reference -- None of the following candidates is applicable because of receiver type mismatch
            Text(text = "My app")
        }
    }
}
// gradle (copied from Android docs)
dependencies {
    def composeBom = platform('androidx.compose:compose-bom:2023.10.00')
    implementation composeBom
    androidTestImplementation composeBom

    // Choose one of the following:
    // Material Design 3
    implementation 'androidx.compose.material3:material3'
    // or Material Design 2
    implementation 'androidx.compose.material:material'
    // or skip Material Design and build directly on top of foundational components
    implementation 'androidx.compose.foundation:foundation'
    // or only import the main APIs for the underlying toolkit systems,
    // such as input and measurement/layout
    implementation 'androidx.compose.ui:ui'

    // Android Studio Preview support
    implementation 'androidx.compose.ui:ui-tooling-preview'
    debugImplementation 'androidx.compose.ui:ui-tooling'

    // UI Tests
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
    debugImplementation 'androidx.compose.ui:ui-test-manifest'

    // Optional - Included automatically by material, only add when you need
    // the icons but not the material library (e.g. when using Material3 or a
    // custom design system based on Foundation)
    implementation 'androidx.compose.material:material-icons-core'
    // Optional - Add full set of material icons
    implementation 'androidx.compose.material:material-icons-extended'
    // Optional - Add window size utils
    implementation 'androidx.compose.material3:material3-window-size-class'

    // Optional - Integration with activities
    implementation 'androidx.activity:activity-compose:1.8.0'
    // Optional - Integration with ViewModels
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1'
    // Optional - Integration with LiveData
    implementation 'androidx.compose.runtime:runtime-livedata'
    // Optional - Integration with RxJava
    implementation 'androidx.compose.runtime:runtime-rxjava2'

    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
}

Solution

  • androidx.activity.compose.setContent is defined as an extension method on ComponentActivity, so make sure you're calling it from (or at least on) an Activity subclass that extends ComponentActivity:

    class MyClass: ComponentActivity() {