我已经构建了一个片段,该片段基本上包含一个Recyclerviewer以及一个使用数据绑定和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)
在搜索了我的问题之后,我发现的唯一建议是即使使用的是ViewModel类而不是AndroidViewModel,也要实现ViewModelFactory。
请参见This answer
ArticleViewModel类
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?
答案 0 :(得分:3)
ViewModel
系统仅知道如何在ViewModel
子类(或AndroidViewModel
子类的单参数构造函数)上使用零参数构造函数。因此,要么:
根本没有构造函数,或者
具有一个显式的零参数构造函数(但请考虑摆脱另一个变量,因为没有东西应该使用它),或者
实现工厂,因此您可以调用所需的自定义构造函数
答案 1 :(得分:0)
通常,如果要通过ViewModel
创建ViewModelProviders.of(activity).get(ArticleViewModel::class.java)
的实例,则它期望ViewModel
的零参数构造函数。如果要将任何依赖项传递给ViewModel
构造函数,则必须使用工厂,然后可以通过ViewModelProviders.of(activity, factory).get(ArticleViewModel::class.java)
进行实例化。
建议:
private var articlesLiveData = MutableLiveData<List<Article>>()
的列表,而不是ViewModels的列表。