Search code examples
androidkotlinandroidxandroid-jetpackandroid-architecture-components

Type mismatch: inferred type is LoginActivity but LifecycleOwner was expected


I am playing with the preview version of AndroidStudio - 3.4 Canary 9.

I selected the default login activity option from Configure your project window and provided, selected below options:

Configure your project

No error reported on Gradle sync, however on build compilation it produces following error:

Type mismatch: inferred type is LoginActivity but LifecycleOwner was expected

Here is the code snippet on which it is showing the error:

// Type mismatch at this@LoginActivity, required LifeCycleOwner, found - LoginActivity
        loginViewModel.loginFormState.observe(this@LoginActivity, Observer {
            val loginState = it ?: return@Observer

            // disable login button unless both username / password is valid
            login.isEnabled = loginState.isDataValid

            if (loginState.usernameError != null) {
                username.error = getString(loginState.usernameError)
            }
            if (loginState.passwordError != null) {
                password.error = getString(loginState.passwordError)
            }
        })

LoginActivity is derived from AppCompatActivity()

Respective imported library is androidx.appcompat.app.AppCompatActivity

Here is content from app level build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.socialsample"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.1.0-alpha03'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.annotation:annotation:1.0.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

Here is content from Project level build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.11'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.0-alpha09'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Am I missing anything over here? Please suggest.


Solution

  • I had the same issue and fix it with updating my support version to

    versions.support = "1.1.0-alpha01"

    so which means just update the

    implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'

    and it should be fixed