android - View 模型 : Unresolved reference

标签 android kotlin android-viewmodel

我想在我的应用中实现 ViewModel,但无法导入 viewModels()activityViewModels()。尝试遵循教程,但它对我来说仍然是 Unresolved reference 。

private val userViewModel: ProfileFlowFragment.UserViewModel by viewModels()

进口:

implementation "androidx.fragment:fragment-ktx"
implementation "androidx.activity:activity-ktx"

// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"

最佳答案

您是否在实现“androidx.fragment:fragment-ktx”中指定了版本? 不管怎样,我的 Gradle 文件中有这些依赖项,一切都很好。

 implementation "androidx.core:core-ktx:$corektxVersion"
 implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_stdlibVersion"

同时添加

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

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

相关文章:

android - 单元测试: Observer onChanged should be called twice instead of once

android - 任务 ':app:transformClassesWithJarMergingForDebug'的执行失败

android - react native Android : unable to generate signed APK : "no such scale"

Android - 不同存储选项的生命周期

interface - 带主体的 Kotlin 抽象方法

android - 使用参数查询 ROOM 数据库后无法让 ViewModel 显示数据 (Kotlin)

android - 如何在kotlin中使用协程每秒调用一个函数

android - 无法确定任务 ':app:dokka' 的依赖关系

android - 可以像在导航组件中一样在 android 中的首选项上添加动画

安卓 : How to write a unit test for fragment depending on a viewmodel Live data attribute?