Mohit Goel
Mohit Goel

Reputation: 742

Cannot create an instance of custom ViewModel

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);

My factory 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");
}

My log

  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)

Upvotes: 16

Views: 12883

Answers (6)

Paras Sidhu
Paras Sidhu

Reputation: 670

I'm a proficient Android developer and I have used ViewModel 100s of times with no issue. Today I came across this issue. Spent hours and scrolled through various SO posts. Didn't get solved.

Then I saw that the package name in which I have the ViewModel contains new. Like this:

com.myapp.myfeature.new.feature

I changed new to neww for testing like this: com.myapp.myfeature.neww.feature

and it worked! I hope someone find it useful.

Upvotes: 0

Yamini
Yamini

Reputation: 792

Extend AndroidViewModel from your ViewModel class.

public class YourViewModel extends AndroidViewModel {

    public YourViewModel(Application application) {
        super(application);

        //Todo: ...
    }
}

Upvotes: 0

Faustino Gagneten
Faustino Gagneten

Reputation: 2774

The solution for me was injecting the activity because I was using Dagger2

        AndroidInjection.inject(this);

Upvotes: 3

Suman
Suman

Reputation: 1313

Need to add below code in all activity if you use multiple activity

AndroidInjection.inject(this);

Upvotes: 2

Shahid Ahmad
Shahid Ahmad

Reputation: 92

Replace:

private MoviesDataViewModel mViewModel;

with:

MoviesDataViewModel mViewModel;

Upvotes: -4

azizbekian
azizbekian

Reputation: 62189

Instead of:

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

Perform:

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

Upvotes: 10

Related Questions