gpt4 book ai didi

java - AndroidViewModel 没有零参数构造函数

转载 作者:行者123 更新时间:2023-12-02 11:23:58 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





AndroidViewModel instantiated without passing application context?

(2 个回答)


1年前关闭。




我正在尝试在 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;
}
}

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

这是我请求实例的主要 Activity 。
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"

最佳答案

如果您使用 hilt ,您可能忘记使用 @AndroidEntryPoint 注释您的 Activity

关于java - AndroidViewModel 没有零参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61370134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com