0

I have an app that had been working fine up until now using ROOM and MVVM. I have updated the SDK to 33 and am now getting an issue with the changes to ViewModelProvider.

This is my ViewModel

public class SwimEntryViewModel extends AndroidViewModel {

private final SwimEntryRepository repository;
private final LiveData<List<SwimEntry>> allSwims;

private SwimEntryViewModel (@NonNull Application application) {
    super(application);
    repository = new SwimEntryRepository(application);
    allSwims = repository.getAllSwims();
}


public LiveData<List<SwimEntry>> getAllSwims() { return allSwims; }
public void insert(SwimEntry swimEntry) { repository.insert(swimEntry); }

}

This is my code to get the ViewModel

// Get the ViewModel.
    swimEntryViewModelForYear = new ViewModelProvider(this).get(SwimEntryViewModel.class);
    // Add an observer on the LiveData returned by get.
    // The onChanged() method fires when the observed data changes and the activity is
    // in the foreground.

    swimEntryViewModelForYear.getAllSwims().observe(this, swims -> {
                // Update the cached copy of the words in the adapter.
                setYearSpinner(swims);
                swimsObtained = swims;
            });

These are my dependencies

dependencies {
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.room:room-common:2.4.3'
    implementation 'androidx.sqlite:sqlite:2.2.0'
    implementation 'androidx.room:room-runtime:2.4.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-beta01'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.0-beta01'
    annotationProcessor 'androidx.room:room-compiler-processing:2.4.3'
    annotationProcessor 'androidx.room:room-compiler:2.4.3'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'

and this is the error I am getting

     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.cgem.owswimdiaryxtra.database.SwimEntryViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:314)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278)
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153)
        at com.cgem.owswimdiaryxtra.activities.MainActivity.onCreate(MainActivity.java:142)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
     Caused by: java.lang.NoSuchMethodException: com.cgem.owswimdiaryxtra.database.SwimEntryViewModel.<init> [class android.app.Application]
        at java.lang.Class.getConstructor0(Class.java:2332)
        at java.lang.Class.getConstructor(Class.java:1728)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:312)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278) 
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) 
        at com.cgem.owswimdiaryxtra.activities.MainActivity.onCreate(MainActivity.java:142) 
        at android.app.Activity.performCreate(Activity.java:7994) 
        at android.app.Activity.performCreate(Activity.java:7978) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2022-10-18 12:17:24.647 564-1418/system_process E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
Caro
  • 1
  • 1

0 Answers0