androidviewmodelandroid-architecture-components

Cannot resolve symbol ViewModelProviders on AppCompatActivity


Hey I'm trying to get my ViewModel working, but no luck so far. Android Studio shows error Cannot resolve symbol 'ViewModelProviders'.

Every other question I found on this topic was correcting extends Activity to extends AppCompatActivity, but I am extending the right one. Not sure what I'm missing...
My code is based on This YouTube video

MainActivity.java

public class MainActivity extends AppCompatActivity implements
    TileAdapter.TileAdapterOnClickHandler {


private BaseViewModel viewModel;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //set Toolbar
    Toolbar myToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);


    //initialize viewModel
    viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);

BaseViewModel.java

public class BaseViewModel extends ViewModel {

private Movie[] mMovie;

public void init (Movie[] movies){
    this.mMovie = movies;
}

public Movie[] getMovie() {
    return mMovie;
}

Solution

  • I didn't have both dependencies in my build, hence the problem.

    implementation "android.arch.lifecycle:extensions:1.1.0"
    implementation "android.arch.lifecycle:viewmodel:1.1.0"
    

    Thanks @Muthukrishnan Rajendran