Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 682
Devarshi Avatar asked Jan 03 '19 11:01

Devarshi


3 Answers

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

like image 174
Arutha Avatar answered Oct 20 '22 05:10

Arutha


If you are using androidx.appcompat:appcompat:1.0.2

It won't work for you as LifeCycleOwner is not implemented. Your AppCompatActivity extends FragmentActivity which further extends ComponentActivity and ComponentActivity implements LifeCycleOwner which is not in 1.0.2 version so upgrading (1.1.0-alpha02) or downgrading (1.0.0) will work for you.

Happy coding :)

like image 41
Amit Bhati Avatar answered Oct 20 '22 06:10

Amit Bhati


It seems androidx.appcompat.app.AppCompatActivity doesn't implement LifecycleOwner interface in the version you are using androidx.appcompat:appcompat:1.0.2.

android.support.v7.app.AppCompatActivity implicitly implements LifecycleOwner, so you need to inherit from it instead of androidx.appcompat.app.AppCompatActivity:

class LoginActivity : android.support.v7.app.AppCompatActivity() { ... }

or use some other Activity class that implements (explicitly or implicitly) LifecycleOwner, e.g. android.support.v4.app.FragmentActivity.

Additionally, any custom application class can implement the LifecycleOwner interface. More info about LifecycleOwner is here.

like image 4
Sergey Avatar answered Oct 20 '22 06:10

Sergey