android studio Can't resolve symbol ViewModelProvider

450 views Asked by At

I've encountered this problem of android is not able to identify the ViewModelProvider class , and I've searched here in stackoverflow about this same issue and all of the answers did not help ,

here is my dependencies :

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'

def lifecycle_version = "2.4.0-rc01"
def arch_version = "2.1.0"


// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"

// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

and this is my ViewModel class :

    package com.example.myapplication.UI;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;


import pojo.Movie;

public class MovieViewModel extends ViewModel {

    MutableLiveData<String> movieMutableLiveData= new MutableLiveData<>();

    private Movie getData(){
   return  new Movie("lina ");
    }

    public void getMovieName(){
        String name= getData().getName();
        movieMutableLiveData.setValue(name);
    }
}

and this is my main activity :

    package com.example.myapplication.UI;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.AndroidViewModel;


import com.example.myapplication.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MovieViewModel movieViewModel;

    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.button)
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        button.setOnClickListener(this);

        movieViewModel= new ViewModelProvider(this).get(MovieViewModel.class);

        movieViewModel.movieMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                textView.setText(s);
            }
        });

    }

    @Override
    public void onClick(View v) {
if (v.getId()==R.id.button){
movieViewModel.getMovieName();
}
    }
}

i can't seem to figure out where the problem is , because i copied the same dependencies form the official android website and the problem is still the same , even the answers here in the forum could not fix this problem , please help

0

There are 0 answers