代码之家  ›  专栏  ›  技术社区  ›  Devarshi

类型不匹配:推断类型为Loginactivity,但应为LifecycleOwner

  •  2
  • Devarshi  · 技术社区  · 7 年前

    我正在播放的预览版本是 AndroidStudio - 3.4 Canary 9 .

    我从中选择了默认的登录活动选项 Configure your project 窗口并提供,选择以下选项:

    Configure your project

    Gradle Sync上未报告任何错误,但在编译生成时会产生以下错误:

    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
    }
    

    我这里有什么东西不见了吗?请建议。

    3 回复  |  直到 7 年前
        1
  •  8
  •   Arutha    7 年前

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

    versions.support = "1.1.0-alpha01"

    所以这意味着只要更新

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

    它应该是固定的

        2
  •  1
  •   Sergio    7 年前

    似乎 androidx.appcompat.app.AppCompatActivity 不执行 LifecycleOwner 您使用的版本中的接口 androidx.appcompat:应用程序兼容性:1.0.2 .

    android.support.v7.app.AppCompatActivity 隐式实现 生命财产所有人 ,因此您需要从中继承,而不是从中继承 androidx.appcompat.app.appcompatactivity应用程序兼容活动 :

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

    或者用其他的 Activity 实现的类(显式或隐式) 生命财产所有人 ,例如 android.support.v4.app.FragmentActivity .

    此外,任何自定义应用程序类都可以实现 生命财产所有人 接口。更多信息 生命财产所有人 here .

        3
  •  0
  •   Amit Bhati    7 年前

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

    这对你来说不起作用 生命财产所有人 未实现。 您的appcompatactivity扩展了fragmentActivity,进一步扩展了componentActivity,componentActivity实现的LifecycleOwner不在1.0.2版本中,因此升级(1.1.0-alpha2)或降级(1.0.0)将对您有效。

    快乐编码: