android - Unresolved reference :viewModels

标签 android kotlin viewmodel android-viewmodel kotlin-android-extensions

我是Android Kotlin的新手,我在OCR应用程序中使用viewModel。“import androidx.fragment.app.viewModels”未显示。我已添加所有必要的依赖项。这是怎么做的?
这里是依赖项。

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'



    implementation 'com.google.android.material:material:1.2.1'

    //lottie animation
    implementation 'com.airbnb.android:lottie:3.4.1'

    // Add CameraX dependencies
    def camerax_version = "1.0.0-beta05"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-view:1.0.0-alpha12"

    // Add ML Kit dependencies
    implementation 'com.google.android.gms:play-services-mlkit-text-recognition:16.0.0'
    implementation 'com.google.mlkit:language-id:16.0.0'
    implementation 'com.google.mlkit:translate:16.0.0'



}
这些是导入错误。
enter image description here

最佳答案

添加此依赖项以在Fragment中使用by viewModels():

implementation 'androidx.fragment:fragment-ktx:1.2.5'

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

相关文章:

android - 当应用程序处于后台时,在 Firebase 服务中编辑 SharedPreferences 后,值将会更改

android - 将图像添加到具有形状的图层列表项

android - 带 LiveData 的 ViewModel 与不带 LiveData 的 ViewModel 有何不同?

Spring JPA 无法在 Kotlin 数据类中使用自定义 setter 映射字段

jquery - jQuery 可以将 ViewModel 发送到 ASP.NET MVC 中的 Controller 吗?

android - ViewModel 观察者方法返回 null

android - java.lang.IllegalStateException ConstraintLayout 没有为 fragment 设置 NavController

android - 如何在 Android 中从 JPEG 创建动画 GIF(开发)

kotlin - 移至androidX后出现“inferred type is FragmentManager? but FragmentManager was expected”错误

java - fragment 内的上下文和 Activity 有什么区别?