无法在Kotlin中创建类ViewModel的实例 [英] Cannot create an instance of class ViewModel in Kotlin
问题描述
我正在尝试在Fragment中初始化 viewmodel ,但是每次我想在viewmodel构造函数中传递接口时,都会引发错误无法创建ViewModel类的实例
另外,我在 kotlin-kapt 或任何生命周期注释
是我的ViewModel类
class SettingsViewModel(
var settingsView:SettingsView
):ViewModel(){}
这是我要初始化视图模型的片段
类SettingsFragment:Fragment(),SettingsView {
var viewmodel:SettingsViewModel? = null
覆盖fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
}
覆盖fun onCreateView(
inflater:LayoutInflater,容器:ViewGroup ?,
savedInstanceState:捆绑包?
):视图? {
//为该片段添加布局
viewmodel = ViewModelProviders.of(this).get(SettingsViewModel(this):: class.java)
val binding =
DataBindingUtil.inflate< FragmentSettingsBinding>(inflater,R.layout.fragment_settings,container,false)
.apply {}
返回binding.root
}
初始化ViewModel时通过没有工厂的ViewModelProviders,这意味着您只能实例化没有构造函数参数的ViewModel。像这样:
viewmodel = ViewModelProviders.of(this).get(SettingsViewModel :: class.java)
请注意,由于您没有调用构造函数,因此无法将参数传递给.class调用
由于ViewModel具有构造函数参数,因此需要实现ViewModelProvider.Factory才能使用给定的参数检索其实例。
此处是对Android开发人员的引用: https://developer.android.com/reference/ android / arch / lifecycle / ViewModelProvider
此处指的是一篇可以为您提供有关如何实现工厂的见解的文章: https://medium.com/ @ marco_cattaneo / android-viewmodel-and-factoryprov使用dagger-2-d9e20a07084c的ider-好的管理方法 I'm trying to initialize viewmodel in Fragment but each time I want to pass the interface in my viewmodel constructor it throws the error Cannot create an instance of class ViewModel
Also I don't have any problem in kotlin-kapt or any lifecycle annotations here is my ViewModel class and here is my fragment where i want to initialize the viewmodel
When you are initialising the ViewModel through ViewModelProviders without a factory, this means that you can only instantiate a ViewModel which has no constructor arguments. Like this: Notice that you cannot pass arguments to a .class call because you are not calling the constructor Since your ViewModel has constructor arguments you need to implement a ViewModelProvider.Factory in order to be able to retrieve its instance with given parameters. Here is a reference to android devs: https://developer.android.com/reference/android/arch/lifecycle/ViewModelProvider Here refers to an article that can give you a insight on how to implement the factory: https://medium.com/@marco_cattaneo/android-viewmodel-and-factoryprovider-good-way-to-manage-it-with-dagger-2-d9e20a07084c 这篇关于无法在Kotlin中创建类ViewModel的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!class SettingsViewModel (
var settingsView: SettingsView
) : ViewModel(){ }
class SettingsFragment : Fragment(), SettingsView {
var viewmodel :SettingsViewModel? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
viewmodel = ViewModelProviders.of(this).get(SettingsViewModel(this)::class.java)
val binding =
DataBindingUtil.inflate<FragmentSettingsBinding>(inflater, R.layout.fragment_settings, container, false)
.apply{}
return binding.root
}
viewmodel = ViewModelProviders.of(this).get(SettingsViewModel::class.java)