Jochen
Jochen

Reputation: 1776

ViewModelProvider() creates an error, although own activity is extending AppCompatActivity

Currently, I want to follow the Android Tutorial here: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#13

I wanted to include the idea of the tutorial into my own project and created my own activity "WordsList" and extended the same classes like the tutorial. All went fine, but "this" in the last line of my snippet throws an error "Cannot resolve constructor ViewModelProvider(...)". In my opinion that makes absolutely no sense, because both classes MainActivity and WordsList are extending the same class?! To dig deeper I downloaded the github project and opened in Android Studio and started it on my phone. It worked! I am absolutely clueless what can cause this difference.

The corresponding snippet of MainActivity is here on github: https://github.com/googlecodelabs/android-room-with-a-view/blob/master/app/src/main/java/com/example/android/roomwordssample/MainActivity.java

Here is my snippet:

public class WordsList extends AppCompatActivity {

// Room DB strategies ...
private WordViewModel wordViewModel;
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1; // Request code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_words);

    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    final WordListAdapter adapter = new WordListAdapter(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    // Get a new or existing ViewModel from the ViewModelProvider.
    wordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

Upvotes: 1

Views: 2572

Answers (5)

M408
M408

Reputation: 33

Had the same problem, updated the

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

to

    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

The constructor ViewModelProvider(ViewModelStoreOwner owner) is introduced in 2.2.0 The docs are a little iffy here

Upvotes: 2

Aman Aalam
Aman Aalam

Reputation: 11251

According to the docs, the following way is deprecated: ViewModelProviders.of(this).get(MyViewModel.class);

This is to be used now:
MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

My only problem was that I hadn't exported this dependency in my module gradle file:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-beta01"

As soon as I included this dependency, all my issues went away.

Upvotes: 5

John De la cruz
John De la cruz

Reputation: 96

Did you add the provider into the manifest?

     <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.my.own.app.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
        <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>


    </provider>

Upvotes: 0

Jochen
Jochen

Reputation: 1776

I found the solution, these days you need to call

// Get a new or existing ViewModel from the ViewModelProvider.
wordViewModel = ViewModelProviders.of(this).get(WordViewModel.class);

Upvotes: 0

Yeonsan
Yeonsan

Reputation: 21

You have to make sure you are using the 'new' androidx. Here's a link to the google docs to migrate your current project to an androidx one.

Upvotes: 0

Related Questions