AndroidViewModel 没有零参数构造函数

我正在尝试在 MainActivity 中创建我的 AndroidViewModel 的实例。当我这样做时,我得到以下错误没有零参数构造函数

这是我的食谱ViewModel

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;
    }
}

如果我错了,现在请纠正我,但AndroidViewModel需要在构造函数中提供应用程序上下文,而ViewModel不需要。所以我不知道为什么android要求零参数构造函数。

这是我的主要活动,我要求一个实例。

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();
            }
        });
    }
}

这是我的实现。

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"

答案 1

如果您正在使用 ,则可能忘记用 注释您的活动hilt@AndroidEntryPoint


答案 2

显然,如果我改变

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

到这个

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

它的工作原理。我不知道为什么这个解决方案有效,有人可以解释一下吗?


推荐