Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidViewModel has no zero argument constructor [duplicate]

I am trying to create an instance of my AndroidViewModel in MainActivity. When I do this I get the following error has no zero argument constructor

Here is my RecipeViewModel

package com.example.kookrecepten;

import android.app.Application;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;

import java.util.List;

public class RecipeViewModel extends AndroidViewModel {
    private RecipeRepository recipeRepository;
    private LiveData<List<Recipe>> allRecipes;

    public RecipeViewModel(Application application) {
        super(application);
        recipeRepository = new RecipeRepository(application);
        allRecipes = recipeRepository.getAllRecipes();
    }

    public void insert(Recipe recipe) {
        recipeRepository.insert(recipe);
    }

    public void update(Recipe recipe) {
        recipeRepository.update(recipe);
    }

    public void delete(Recipe recipe) {
        recipeRepository.delete(recipe);
    }

    public void deleteAll() {
        recipeRepository.deleteAllRecipes();
    }

    public LiveData<List<Recipe>> getAllRecipes() {
        return allRecipes;
    }
}

Now correct me if I'm wrong but AndroidViewModel needs the Application context in the constructor and ViewModel doesn't. So I have no idea why android asks for a zero arguments constructor.

Here is my Main activity where I ask for an instance.

package com.example.kookrecepten;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.kookrecepten.databinding.ActivityMainBinding;

import java.util.List;


public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private RecipeViewModel recipeViewModel;

    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        //Get an instance of the RecipeViewModel.
        recipeViewModel = new ViewModelProvider(this).get(RecipeViewModel.class);
        recipeViewModel.getAllRecipes().observe(this, new Observer<List<Recipe>>() {
            @Override
            public void onChanged(List<Recipe> recipes) {
                //Update recycle view

                Toast.makeText(MainActivity.this, "triggered", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

And this are my implementations.

def lifecycle_version = "2.2.0"
def room_version = "2.2.5"

//LifeCycle Components
    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
    // Annotation processor
    implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"

    //Room Components
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
like image 617
Ruben Avatar asked Apr 22 '20 16:04

Ruben


1 Answers

apparently if I change

recipeViewModel = new ViewModelProvider(this).get(RecipeViewModel.class);

to this

recipeViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(RecipeViewModel.class);

It works. I don't know why this solution works could someone explain?

like image 132
Ruben Avatar answered Sep 23 '22 01:09

Ruben