为什么为我的 viewModel 类实现 ViewModelProvider.Factory 很重要?



>我已经构建了一个片段,该片段基本上包含一个使用数据绑定和 LiveData(这是我经常实现的代码(的数据类型的自定义 ViewModel,但此时发生了运行时异常错误!

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tdm_project, PID: 9460
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tdm_project/com.example.tdm_project.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.tdm_project.viewmodel.ArticleViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2799)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1537)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6269)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.tdm_project.viewmodel.ArticleViewModel
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:154)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:211)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:135)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:103)
at com.example.tdm_project.HomeFragment.onCreateView(HomeFragment.kt:75)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6683)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2697)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2799) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1537) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:203) 
at android.app.ActivityThread.main(ActivityThread.java:6269) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.tdm_project.viewmodel.ArticleViewModel> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)

在谷歌搜索我的问题之后,我发现的唯一建议是实现ViewModelFactory,即使我使用的是ViewModel类而不是AndroidViewModel。

查看此答案

文章视图模型类


class ArticleViewModel : ViewModel {
//lists
private var articleMList = MutableLiveData<ArrayList<ArticleViewModel>>()
private var articleInnerList = ArrayList<ArticleViewModel>()
constructor(
article: Article
) : super() {
//const with parameters
}
//to observe my list
fun getArticles() : MutableLiveData<ArrayList<ArticleViewModel>>{
articleMList.value = articleInnerList
return articleMList
}
//retrieve data from backend
fun getData() {
//some code
}
}

片段类


class HomeFragment : Fragment() {

//viewmodel
private lateinit var vmodel : ArticleViewModel

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
//set the view
rootView = inflater.inflate(R.layout.home_fragment, container, false)
//a function to initialize my recyclerview
intialiserHorizontally()
//creating the instance of viewmodel
vmodel = ViewModelProviders.of(activity!!).get(ArticleViewModel::class.java)
vmodel.getArticles().observe(this, Observer {
customHAdapter.swapData(it)
})
//getting the data from my db
vmodel.getData()


return rootView
}

我的问题是为什么在这种情况下有必要实现ViewModelProvider.Factory?

就其本身而言,ViewModel系统只知道如何在ViewModel子类上使用零参数构造函数(或AndroidViewModel子类的单参数构造函数(。因此,要么:

  • 根本没有构造函数,或者

  • 有一个显式的零参数构造函数(但请考虑摆脱另一个,因为任何东西都不应该使用它(,或者

  • 实现工厂,以便可以调用所需的自定义构造函数

通常,如果要通过ViewModelProviders.of(activity).get(ArticleViewModel::class.java)创建ViewModel的实例,则需要ViewModel的零参数构造函数。如果要将任何依赖项传递给ViewModel构造函数,则必须使用工厂,然后可以通过ViewModelProviders.of(activity, factory).get(ArticleViewModel::class.java)

.建议:

  1. 使用dagger进行依赖注入(它会让你的生活变得轻松:)(
  2. 在视图模型中使用数据类private var articlesLiveData = MutableLiveData<List<Article>>()列表,而不是视图模型列表。

相关内容

  • 没有找到相关文章

最新更新