gpt4 book ai didi

android - ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java)要求 Activity 而不是get()调用中的viewmodel

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

由于无法预见的原因,ViewModelProviders.of(this).get()失败
这是从FragmentActivity继承的主要 Activity 中调用的。我实际上在.get()调用中遇到错误。该错误表明,它期望的是 Activity 而不是违背文档的ViewModel。我已经清除并禁用了缓存。我的猜测是我导入错误或旧版本的库

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.apsoftware.urbandictionary.R

class DefinitionActivity : AppCompatActivity() {

private lateinit var binding: ActivityPostListBinding
private lateinit var viewModel: DefinitionActivity


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.postList.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
viewModel = ViewModelProviders.of(this).get(DefinitionViewModel::class.java)
binding.viewModel = viewModel
} }

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.core:core-ktx:1.1.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation "com.squareup.okhttp3:okhttp:3.12.0"
implementation "com.squareup.retrofit2:retrofit:2.5.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
implementation 'com.squareup.moshi:moshi:1.6.0'
kapt 'com.android.databinding:compiler:3.1.4'
implementation "com.google.dagger:dagger:2.20"
kapt "com.google.dagger:dagger-compiler:2.11"
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
compileOnly "org.glassfish:javax.annotation:3.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.2"
implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
implementation "android.arch.lifecycle:extensions:1.1.1"

最佳答案

你已经宣布

private lateinit var viewModel: DefinitionActivity

viewModelDefinitionActivity的实例。它应该是一个 DefinitionViewModel,以匹配您传递给 get()的内容。

关于android - ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java)要求 Activity 而不是get()调用中的viewmodel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586913/

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