gpt4 book ai didi

android - Unresolved reference : viewModels()

转载 作者:行者123 更新时间:2023-12-02 12:09:32 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





ViewModel in Kotlin: Unresolved Reference

(12 个回答)


2年前关闭。




我刚刚启动了一个 Android 应用程序,我想使用 ViewModel。我查看了文档并将所有依赖项放在 Lifecycle 中但是当我尝试实例化像这样的 View 模型 MyViewModel by viewModels()我收到此错误“ Unresolved reference :viewModels()”。

这些是依赖项:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"

implementation "android.arch.lifecycle:extensions:1.1.1"
kapt "android.arch.lifecycle:compiler:1.1.1"

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"

implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:$lifecycle_version"
testImplementation "androidx.arch.core:core-testing:$arch_version"

}

最佳答案

将此包含在您的依赖项中,

implementation "android.arch.lifecycle:extensions:1.1.1"

然后,添加 apply plugin: 'kotlin-kapt'在 build.gradle

然后加,
kapt "android.arch.lifecycle:compiler:1.1.1"

替换其他 annotationProcessorkapt
看看有没有用,如果没有,那就去

file -> invalidate cache and restart

关于android - Unresolved reference : viewModels(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61498013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com