问题描述
我已经构建了一个片段,该片段基本上包含一个Recyclerviewer以及一个使用数据绑定和LiveData(这是我经常实现的代码)的针对我的数据类型的自定义ViewModel,但是这一次发生了运行时异常错误!
I have built a fragment that basically contains a Recyclerviewer with a custom ViewModel for my Data type using data binding and LiveData (which is a piece of code that I frequently implement) but in this time a runtime exception error has occurred!
/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)
在搜索了我的问题之后,我发现的唯一建议是即使使用的是ViewModel类而不是AndroidViewModel,也要实现ViewModelFactory.
After googling my problem, the only suggestion I found is to implement a ViewModelFactory even though am using a ViewModel class and not AndroidViewModel.
请参见此答案
ArticleViewModel类
ArticleViewModel class
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?
My question is why it is necessary to implement a ViewModelProvider.Factory in this case?
推荐答案
ViewModel
系统仅知道如何在ViewModel
子类上使用零参数构造函数(或用于单参数构造函数). AndroidViewModel
子类).因此,要么:
On its own, the ViewModel
system only knows how to use a zero-argument constructor on your ViewModel
subclasses (or a single-parameter constructor for AndroidViewModel
subclasses). So, either:
-
根本没有构造函数,或者
Have no constructors at all, or
具有一个显式的零参数构造函数(但请考虑摆脱另一个变量,因为什么都不要使用它),或者
Have an explicit zero-argument constructor (but consider getting rid of your other one, as nothing should use it), or
实现工厂,因此您可以调用所需的自定义构造函数
Implement a factory, so you can call your desired custom constructor
这篇关于为什么为我的viewModel类实现ViewModelProvider.Factory很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!