Sheler
Sheler

Reputation: 3969

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;
}

Upvotes: 139

Views: 92143

Answers (15)

Victor Sam VS
Victor Sam VS

Reputation: 346

Remove this lines

    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.8.4")
implementation("androidx.lifecycle:lifecycle-view-model-ktx:2.8.4")

Upvotes: 0

hata
hata

Reputation: 12478

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.

Upvotes: 10

Sumit Shukla
Sumit Shukla

Reputation: 4494

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)

Upvotes: 33

aksBD
aksBD

Reputation: 199

-'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

Upvotes: 4

Mihai
Mihai

Reputation: 26784

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

Upvotes: 4

Zeeshan Akhtar
Zeeshan Akhtar

Reputation: 521

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.

Upvotes: 2

s wang
s wang

Reputation: 21

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

Upvotes: 2

SedJ601
SedJ601

Reputation: 13859

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.

Upvotes: 0

Babao
Babao

Reputation: 1313

If you are using androidx you need this:

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

Upvotes: 115

duyuanchao
duyuanchao

Reputation: 4303

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"

Upvotes: 5

Sebastian Duran
Sebastian Duran

Reputation: 115

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

Upvotes: 2

Fatih Çakıroğlu
Fatih Çakıroğlu

Reputation: 503

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"

Upvotes: 5

Sanjeev
Sanjeev

Reputation: 4375

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"
}

Upvotes: 15

Sulav Timsina
Sulav Timsina

Reputation: 843

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

Upvotes: 3

Sheler
Sheler

Reputation: 3969

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

Upvotes: 221

Related Questions