Cannot resolve symbol ViewModelProviders on AppCompatActivity
Asked Answered
N

14

139

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;
}
Nicolella answered 21/3, 2018 at 11:50 Comment(4)
"Cannot resolve symbol" means that either you do not have the import statement, or you do but you do not have the dependency in your Gradle setup.Shumway
Check do you have dependecy for android.arch.lifecycle:extensions in build.gradleErleneerlewine
Either he is using some library and added the dependency in gradle file so he is able to import ViewModel or he has custom class ViewModel under the different package and he is importing it from thereHouseline
Didn't have extensions as dependency. Thanks @MuthukrishnanRajendranNicolella
N
221

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

Nicolella answered 21/3, 2018 at 13:7 Comment(7)
@Nicolella You're answer is correct but the docs is wrong. In the documentation it is specified that ViewModel and LiveDat both are in implementation "android.arch.lifecycle:extensions:1.1.1"Razzledazzle
Hmm this solution has still did not solved my issue. I have implementation 'android.arch.lifecycle:extensions:1.1.1' in my app gradle and allprojects { repositories { google() jcenter() } } Is there something else that needs added?Underneath
I see ViewModelProvider class but it does not have the .of() method. ViewModelProviders is still not defined. I am on a mac if that matters.Underneath
Is there a min sdk or java 8 definition that needs to be added to the project?Underneath
Never mind. Make sure you sync your gradles files if it does not show up.Underneath
@Sheler. Doesn't work for me. By the way my gradle files have this support library : 'com.android.support:appcompat-v7:27.1.1'. Does that affect?Sapper
@Underneath this is for ViewModelProviders not ViewModelProviderForelli
G
114

If you are using androidx you need this:

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
Grandson answered 3/10, 2018 at 2:45 Comment(3)
For anybody else having this problem, you also need this in your app gradle: apply plugin: 'androidx.navigation.safeargs' and this in your android gradle: classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha06" If you have trouble where to put these things, look in the android-sunflower demo app from google.Motherwell
check this link for the latest androidx version developer.android.com/jetpack/androidx/migrateDistributor
What's the stupidity! ViewModelProvider is available without dependency but for ViewModelProviders we need an extra dependency.Quintile
B
33

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:

Java

Yourclass obj = new ViewModelProvider(context).get(ClassViewModel.class);

Kotlin

var obj = ViewModelProvider(context).get(ClassViewModel::class.java)
Basement answered 2/3, 2020 at 15:53 Comment(1)
Good, but the ´owner´ may not be a ´context´, if this is created in a Fragment.Lashkar
R
15

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"
}
Riddick answered 22/9, 2018 at 8:48 Comment(2)
To know latest version of the android.arch.lifecycle:extensions you can search extensions in maven.google.com/web/index.html and see latest versionRiddick
androidx.lifecycle:lifecycle-extensions is deprecated developer.android.com/jetpack/androidx/releases/lifecycleToast
I
10

In my case (Android Studio 3.6.3 ~ 4.1.2), in AppCompatActivity to successfully do:

MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

it requires both of these:

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

(alone with lifecycle-extentions is not sufficient)

Update

It seems that you have no longer to include the gradle implementation line ('androidx.lifecycle:lifecycle-extensions:2.2.0') to instantiate ViewModelProvider.

Inflammation answered 21/4, 2020 at 7:43 Comment(1)
androidx.lifecycle:lifecycle-extensions is deprecated developer.android.com/jetpack/androidx/releases/lifecycleToast
U
5

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"
Upholstery answered 6/2, 2019 at 8:29 Comment(0)
R
5

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"
Reg answered 20/7, 2019 at 14:32 Comment(0)
P
4

In implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" and up ViewModelProviders is deprecated,use

viewModel = ViewModelProvider(this).get(BaseViewModel.class);

or in Kotlin

viewModel = ViewModelProvider(this).get(BaseViewModel::class.java);

instead of

viewModel = ViewModelProviders.of(this).get(BaseViewModel.class);
Pied answered 1/4, 2020 at 7:37 Comment(1)
just the right info that was needed. thanks a lotJody
C
3

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

Circularize answered 16/9, 2018 at 17:1 Comment(0)
C
3

-'ViewModelProviders' is deprecated now

  • Now alternative
  • In java

viewModel = ViewModelProvider(this).get(BaseViewModel.class);

  • In kotlin

var viewModel = ViewModelProvider(this).get(BaseViewModel::class.java)

Refs - https://developer.android.com/reference/androidx/lifecycle/ViewModelProviders

Cynthy answered 5/5, 2020 at 11:32 Comment(0)
W
2

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
Windbreak answered 29/3, 2019 at 22:31 Comment(0)
Q
2

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

Quantitative answered 6/2, 2020 at 11:25 Comment(0)
M
2

In My Case, I am facing below issue i.e:

** cannot access androidx.lifecycle.has a default viewmodelprovider factory which is a enter image description heresubclass of your class Name, check your module classpath check your model conflicting dependencies **

I added below dependencies in my project build.gradle.

def lifecycle_version = "2.2.0"

implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"

implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

Then I create my class in a module project and I'm facing this issue, then I add these libraries in module build.gradle file and the issue is resolved.

Matti answered 25/3, 2020 at 5:53 Comment(0)
M
0

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.

Madelyn answered 17/10, 2019 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.