Home » Android » android – Cannot resolve symbol ViewModelProviders on AppCompatActivity

android – Cannot resolve symbol ViewModelProviders on AppCompatActivity

Posted by: admin March 11, 2020 Leave a comment

Questions:

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;
}
How to&Answers:

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

Answer:

If you are using androidx you need this:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

Answer:

If you are using compiled sdk version 28 or higher you only need to add single dependecy to get ViewModel and LiveData

dependencies {
    //...
    def lifecycle_version = "1.1.1"

    // ViewModel and LiveData
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
}

Answer:

you should add library in your project’s build.gradle

def lifecycle_version = “2.0.0”

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

Answer:

I solve this problem from Android official documentation. Add below to build.grale

def lifecycle_version = "2.0.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

Answer:

In the build.gradle file, add these lines in the dependencies block

dependencies {
...
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
//if not using java 8,use the following line
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
//if using java 8,ignore above line and add the following line
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
...
}

Sample Image of build.gradle file

Answer:

Use androix libraries

Change

implementation 'com.android.support:appcompat-v7:28.0.0'

to

implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'

You can use

Refactor>Migrate to AndroidX

Answer:

android.arch.lifecycle:extensions is deprecated use

def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

Create instance of viewmodel like this:

Yourclass obj = new ViewModelProvider(AnotherActivity.this).get(Yourclass.class);

Make sure your class is public and it’s constructor is present as public.

Answer:

I had the same problem. None of the other solutions helped me.

I realized that I was using import androidx.lifecycle.ViewModelProvider; instead of import androidx.lifecycle.ViewModelProviders;.

So make sure you are using import androidx.lifecycle.ViewModelProviders;. That is ViewModelProviders with an s.

Answer:

Works fine in my app(java)
before

loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory())
                .get(LoginViewModel.class);

changes to

loginViewModel = new ViewModelProvider(this,new LoginViewModelFactory()).get(LoginViewModel.class);

hope it could help