无法创建自定义ViewModel的实例

无法创建自定义ViewModel的实例

本文介绍了无法创建自定义ViewModel的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dagger2库.每当我尝试运行我的项目时,都会说无法创建视图模型类的实例.

I am using dagger2 library. whenever I am trying to run my project is says not able to create instance of view model class.

我试图创建实例的主要活动

main activity where I am trying to create an instance

   ((MovieApplication) getApplication()).getAppComponent().inject(this);
    mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);

我的工厂班级

public class ViewModelFactory implements ViewModelProvider.Factory {
private MoviesDataViewModel mViewModel;

@Inject
public ViewModelFactory(MoviesDataViewModel viewModel) {
    this.mViewModel = viewModel;
}

@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
    if (modelClass.isAssignableFrom(MoviesDataViewModel.class)) {
        return (T) mViewModel;
    }
    throw new IllegalArgumentException("Unknown class name");
}

我的日志

  Caused by: java.lang.RuntimeException: Cannot create an instance of class com.moviedata.viewmodel.MoviesDataViewModel
                                                               at android.arch.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:145)
                                                               at android.arch.lifecycle.ViewModelProviders$DefaultFactory.create(ViewModelProviders.java:143)
                                                               at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:128)
                                                               at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:96)
                                                               at com.moviedata.ui.MainActivity.onCreate(MainActivity.java:28)
                                                               at android.app.Activity.performCreate(Activity.java:6321)
                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535) 
                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:154) 
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 
                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                               at android.os.Looper.loop(Looper.java:148) 
                                                               at android.app.ActivityThread.main(ActivityThread.java:5582) 
                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                            Caused by: java.lang.InstantiationException: java.lang.Class<com.moviedata.viewmodel.MoviesDataViewModel> has no zero argument constructor
                                                               at java.lang.Class.newInstance(Native Method)

推荐答案

而不是:

mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class);

执行:

mViewModel = ViewModelProviders.of(this, viewModelFactory).get(MoviesDataViewModel.class);

这篇关于无法创建自定义ViewModel的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:46