类型不匹配:推断类型为 LoginActivity,但预期为 LifecycleOwner

Type mismatch: inferred type is LoginActivity but LifecycleOwner was expected

我正在玩 AndroidStudio - 3.4 Canary 9 的预览版。

我从 Configure your project window 中选择默认登录 activity 选项并提供,选择以下选项:

Gradle 同步没有错误报告,但是在构建编译时会产生以下错误:

Type mismatch: inferred type is LoginActivity but LifecycleOwner was expected

这是显示错误的代码片段:

// 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 派生自 AppCompatActivity()

各自导入的库是androidx.appcompat.app.AppCompatActivity

这是来自应用级别的内容 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'
}

这是项目级别的内容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
}

我在这里遗漏了什么吗?请提出建议。

似乎 androidx.appcompat.app.AppCompatActivity 没有在您使用的版本 androidx.appcompat:appcompat:1.0.2 中实现 LifecycleOwner 接口。

android.support.v7.app.AppCompatActivity隐式实现了LifecycleOwner,所以你需要继承它而不是androidx.appcompat.app.AppCompatActivity:

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

或使用其他一些 Activity class 实现(显式或隐式)LifecycleOwner,例如android.support.v4.app.FragmentActivity

此外,任何自定义应用程序 class 都可以实现 LifecycleOwner 接口。有关 LifecycleOwner 的更多信息是 here

我有同样的问题并通过将我的支持版本更新到

来解决它

versions.support = "1.1.0-alpha01"

所以这意味着只需更新

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

而且应该修复

如果您正在使用 androidx.appcompat:appcompat:1.0.2

它对您不起作用,因为 LifeCycleOwner 未实现。 您的 AppCompatActivity 扩展了 FragmentActivity,后者进一步扩展了 ComponentActivity,而 ComponentActivity 实现了 LifeCycleOwner,它不在 1.0.2 版本中,因此升级 (1.1.0-alpha02) 或降级 (1.0.0) 将对您有效。

编码愉快:)