Francesco
Francesco

Reputation: 15

Cannot resolve symbol 'ViewModelProvider'

I wanted to use android studio's given Login Activity (File>New>Activity>Login Activity), but when I added the activity I got the error for ViewModelProvider. I tried looking for the solution but everything I found was mostly related to ViewModelProviders (with final "s", deprecated) or said to add certain dependencies to the build module, neither of which worked. Any help please?

import androidx.lifecycle.ViewModelProvider;
public class LoginActivity extends AppCompatActivity {

    private LoginViewModel loginViewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        loginViewModel = new ViewModelProvider(this, new LoginViewModelFactory())
                .get(LoginViewModel.class);
    //...
    }
//...
}

My code looks something like this, I get the error from the import (and every instance) of ViewModelProvider. I already tried to add the dependencies suggested and synced, but nothing seems to work!

Upvotes: 0

Views: 3260

Answers (2)

islamux
islamux

Reputation: 364

I had the same problem(in java):

my gradle:

android {
    namespace 'com.islamux.pokemon'
    compileSdk 34

    defaultConfig {
        applicationId "com.islamux.pokemon"
        minSdk 21
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation libs.appcompat
    implementation libs.material
    implementation libs.activity
    implementation libs.constraintlayout
    testImplementation libs.junit
    androidTestImplementation libs.ext.junit
    androidTestImplementation libs.espresso.core

    def lifecycle_version = "2.7.0"
    def arch_version = "2.2.0"

    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    // ViewModel utilities for Compose
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    // Saved state module for ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"

    // optional - ProcessLifecycleOwner provides a lifecycle for the whole application process
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

and solve it like these in mainActivity:

// old code --> deprecated
pokemonViewModel = new ViewModelProviders.of(this).get(PokemonViewModel.class);

// new code
pokemonViewModel= new ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(PokemonViewModel.class);

Upvotes: 0

Vahid Mohtasham
Vahid Mohtasham

Reputation: 90

If you use Jetpack, you should use this:

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

link

Upvotes: 0

Related Questions