Ad

ViewModelProviders Failing Instantation Because "of" Cannot Be Resolved

- 1 answer

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

Answer

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 ().

Ad
source: stackoverflow.com
Ad