android - 在 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)
注意 我使用了活动,但没有工作
解决方案
确保您使用的是DialogFragment
来自支持库的:
android.support.v4.app.DialogFragment
应该导入而不是android.app.DialogFragment
,因为ViewModelProviders.of()
需要Fragment
来自支持库的.
推荐阅读
- apache-spark - Spark-SQL 3.x 默认插入日期列为 1970-01-01
- html - 如何在 HTML 中做一个四舍五入的矩形?不是 CSS
- android - 尝试弹出对话框时应用程序崩溃
- swift - 使用 ZStack 放大手势无响应
- python-3.x - 绘制矩阵时在辅助颜色栏中显示行总和
- c++ - 为什么这个背包问题的解决方案不起作用?
- swiftui - 为什么在 SwiftUI 中应用旋转 3D 效果后图像会变得模糊?
- javascript - 根据 javascript 数组中的百分比分配类别
- javascript - 在一个简单的 Javascript 视频游戏中设置 backgroundImage
- python - 使用 Python 将图像添加到 PDF 文件