ViewModelProviders failing instantation because "of" cannot be resolved

Viewed 45

Im experimenting with view models but, came across an odd error the compiler says it doesn't recognize "of". I already have the the dependency imported in my build.gradle and sycned like so

 def ver = "1.1.1"
implementation "android.arch.lifecycle:extensions:$ver" 

I made sure it is imported correctly in my activity and instantated. Not sure what to do

import android.arch.lifecycle.ViewModelProviders;


 //private BattleRhythmViewModel battleModel;
   battleModel = new ViewModelProviders.of(this).get(BattleRhythmViewModel.class);
3 Answers

Use ViewModelProviders.of(this).get(BattleRhythmViewModel.class)

of is a static method. When you call new ViewModelProviders.of(this), you are tying to create a new instance of ViewModelProviders using constructor, but you never called the constructor using ().

Make sure this is an instance of activity or fragments, Secondly, update the latest version of ViewModel library, Thirdly new keyword is unnecessary since you will not create an instance of ViewModel manually it is provided to you.

for those that other answers not works for them, make sure that you are using ViewModelProviders instead of ViewModelProvider

Related