Cannot resolve symbol ViewModelProviders on AppCompatActivity
Solution 1
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
Solution 2
If you are using androidx
you need this:
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
Solution 3
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)
Solution 4
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"
}
Solution 5
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
.
![Sheler](https://i.stack.imgur.com/nXjmU.jpg?s=256&g=1)
Sheler
PhD. Psychology student; Android Developer; "'You miss 100% of the shots you don't take.' - Wayne Gretzky " - Michael Scott
Updated on June 24, 2021Comments
-
Sheler about 3 years
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
toextends AppCompatActivity
, but I am extending the right one. Not sure what I'm missing...
My code is based on This YouTube videoMainActivity.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; }
-
CommonsWare over 6 years"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. -
Muthukrishnan Rajendran over 6 yearsCheck do you have dependecy for android.arch.lifecycle:extensions in build.gradle
-
Akshay Katariya over 6 yearsEither 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 there -
Sheler over 6 yearsDidn't have extensions as dependency. Thanks @MuthukrishnanRajendran
-
-
Pritish about 6 years@Sheler 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"
-
musterjunk about 6 yearsHmm 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?
-
musterjunk about 6 yearsI see ViewModelProvider class but it does not have the .of() method. ViewModelProviders is still not defined. I am on a mac if that matters.
-
musterjunk about 6 yearsIs there a min sdk or java 8 definition that needs to be added to the project?
-
musterjunk about 6 yearsNever mind. Make sure you sync your gradles files if it does not show up.
-
zulkarnain shah almost 6 years@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?
-
findusl over 5 yearsFor 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.
-
user2301281 over 5 yearscheck this link for the latest androidx version developer.android.com/jetpack/androidx/migrate
-
Sanjeev over 4 yearsTo know latest version of the
android.arch.lifecycle:extensions
you can searchextensions
in maven.google.com/web/index.html and see latest version -
Rahul Rastogi over 4 yearsWhat's the stupidity! ViewModelProvider is available without dependency but for ViewModelProviders we need an extra dependency.
-
Meysam Hadigheh over 4 years@musterjunk this is for ViewModelProviders not ViewModelProvider
-
Jack about 3 years
androidx.lifecycle:lifecycle-extensions
is deprecated developer.android.com/jetpack/androidx/releases/lifecycle -
Jack about 3 years
androidx.lifecycle:lifecycle-extensions
is deprecated developer.android.com/jetpack/androidx/releases/lifecycle