首页 > 解决方案 > 在 Dagger Dialog Fragment 中使用 View Model Providers(ViewModelProviders)

问题描述

当我在 Dagger Fragment 中使用 ViewModelProvider.of 时,我可以毫无问题地使用它,但是在 Dagger Dialg Fragment 中,我在第一个参数(片段)中出现错误

在代码中

viewModel = ViewModelProviders.of(this, viewModelFactory).get(CreatePlanViewModel::class.java)

错误

不能使用提供的参数调用以下函数。

的(片段,ViewmodelProvider.Factory)

of( FragmentActivity ,VIewModelProvider.Factory)

注意 我使用了活动,但没有工作

标签: androidandroid-fragmentsviewmodeldagger-2

解决方案


确保您使用的是DialogFragment来自支持库的:

android.support.v4.app.DialogFragment应该导入而不是android.app.DialogFragment,因为ViewModelProviders.of() 需要Fragment来自支持库的.


推荐阅读