AndroidViewModel 没有零参数构造函数

Rub*_*ben 12 java android android-lifecycle android-studio android-viewmodel

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

这是我的 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我错了,现在纠正我,但 AndroidViewModel 在构造函数中需要 Application 上下文而 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();
            }
        });
    }
}

Run Code Online (Sandbox Code Playgroud)

这是我的实现。

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"
Run Code Online (Sandbox Code Playgroud)

HUS*_*EEM 54

如果您正在使用hilt,您可能忘记用@AndroidEntryPoint

  • 我得到了同样的“没有零参数构造函数”错误,因为我没有将 kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02' 添加到我的 build.gradle 文件中 (4认同)
  • 谢谢,下班后,我收到了你的帖子,我意识到我忘了提及@AndroidEntryPoint。 (2认同)

Rub*_*ben 15

显然如果我改变

recipeViewModel = new ViewModelProvider(this).get(RecipeViewModel.class);
Run Code Online (Sandbox Code Playgroud)

对此

recipeViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(RecipeViewModel.class);
Run Code Online (Sandbox Code Playgroud)

有用。我不知道为什么这个解决方案有效有人可以解释吗?