我想实现ViewModel
到我的应用程序,但我不能导入viewModels()
或activityViewModels()
。试图遵循教程,但它仍然无法解决的参考我。
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"
您在implementation "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'
最新稳定版本如下,应该可以使用
def activity_version = "1.2.0"
def fragment_version = "1.3.0"
implementation "androidx.activity:activity-ktx:$activity_version"
implementation "androidx.fragment:fragment-ktx:$fragment_version"