gpt4 book ai didi

android - Unresolved reference : viewModelScope

转载 作者:行者123 更新时间:2023-12-02 13:12:45 29 4
gpt4 key购买 nike

我正在尝试在我的 android 项目 android studio 中使用 viewModelScope.launch(Dispatchers.IO) 显示错误“ Unresolved reference :viewModelScope”我从官方网站复制粘贴了依赖项。我也尝试迁移到 android x。

我添加了来自官方网站的最新依赖项,还尝试了无效缓存并重新启动,但没有奏效。

我的模块级 gradle 文件依赖项

 dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

def room_version = "2.2.0-alpha01" // 2.1.0 for latest stable version

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor

// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"

// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"

// Test helpers
testImplementation "androidx.room:room-testing:$room_version"



def lifecycle_version = "2.0.0"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx

// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"




}

最佳答案

根据 Lifecycle Release notes ,第一个版本lifecycle-viewmodel-ktx (ViewModel 相关 Kotlin 扩展的正确工件)包含 viewModelScope2.1.0-alpha01 .因此,您应该升级到最新版本:2.1.0-rc012.2.0-alpha02 ,两者都包含该 API。

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

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