Cannot resolve symbol ViewModelProviders on AppCompatActivity
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'.
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
extends Activity
extends AppCompatActivity
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;
import
Check do you have dependecy for android.arch.lifecycle:extensions in build.gradle
– Muthukrishnan Rajendran
Mar 21 '18 at 11:52
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 there– Akshay Katariya
Mar 21 '18 at 11:54
ViewModel
Didn't have extensions as dependency. Thanks @MuthukrishnanRajendran
– Sheler
Mar 21 '18 at 13:08
6 Answers
6
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
@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"
– Nudge
May 8 '18 at 11:16
developer.android.com/topic/libraries/architecture/…
– user1021364
May 20 '18 at 15:05
Reported an issue: issuetracker.google.com/issues/110573923
– Joran Dob
Jun 22 '18 at 9:50
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?
– musterjunk
Jun 26 '18 at 15:03
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.
– musterjunk
Jun 26 '18 at 15:06
If you are using androidx you need this:
androidx
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
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.
– findusl
Oct 24 '18 at 17:34
check this link for the latest androidx version developer.android.com/jetpack/androidx/migrate
– user2301281
Feb 10 at 7:12
If you are using compiled sdk version 28 or higher you only need to add single dependecy to get ViewModel and LiveData
ViewModel
LiveData
dependencies
//...
def lifecycle_version = "1.1.1"
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
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"
...

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"
Use androix libraries
androix
Change
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
to
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
You can use
Refactor>Migrate to AndroidX
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
"Cannot resolve symbol" means that either you do not have the
importstatement, or you do but you do not have the dependency in your Gradle setup.– CommonsWare
Mar 21 '18 at 11:52