gpt4 book ai didi

android - ViewModelFactory can't create an instance 错误创建自定义ViewModelFactory类

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

我正在尝试在 MVVM 架构中使用 Dagger2、Rxjava2、Retrofit2 设计一个应用程序。我在模型(存储库)类中得到响应,我想将其传递给我的 ViewModel,为此我必须设计自己的 ViewModelFactory 类。但我收到无法创建实例错误。这是我的类(class):型号:

private final NetworkService networkService;

public Model(NetworkService networkService) {
this.networkService = networkService;
}

public LiveData<CakeResponse> getCakeList(){

final MutableLiveData<CakeResponse> data = new MutableLiveData<>();

networkService.getTheCakes().enqueue(new Callback<CakeResponse>() {
@Override
public void onResponse(Call<CakeResponse> call, Response<CakeResponse> response) {
data.setValue(response.body());
}

@Override
public void onFailure(Call<CakeResponse> call, Throwable t) {

}
});

return data;

蛋糕 View 模型工厂:

public class CakeViewModelFactory implements ViewModelProvider.Factory{

private final Model model;
private final Application application;

@Inject
public CakeViewModelFactory(Model model, Application application) {

this.model = model;
this.application = application;
}


@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (modelClass.isAssignableFrom(Model.class)) {
return (T) new CakeViewModel(model, application);
}
throw new IllegalArgumentException("Unknown ViewModel class");
}

蛋糕 View 模型:

public class CakeViewModel extends AndroidViewModel {

private final LiveData<CakeResponse> cakeListObservable;

public CakeViewModel(@NonNull Model model, @NonNull Application application) {
super(application);

cakeListObservable = model.getCakeList();
}

public LiveData<CakeResponse> getCakeListObservable() {
return cakeListObservable;
}

蛋糕 Activity

 @Inject
CakeViewModelFactory factory;

@BindView(R.id.recyclerView)
RecyclerView recyclerView;

List<CakeResponseCakes> cakes;
CakesAdapter adapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cakes);

CakeViewModel viewModel = ViewModelProviders.of(this, factory).get(CakeViewModel.class);

DaggerCakeComponent.builder()
.cakeModule(new CakeModule(this))
.applicationComponent(CakeApplication.get(this).getComponent())
.build().inject(this);

ButterKnife.bind(this);

initializeList();

observeViewModel(viewModel);
}

private void observeViewModel(CakeViewModel cakeViewModel){
cakeViewModel.getCakeListObservable().observeForever(new Observer<CakeResponse>() {
@Override
public void onChanged(@Nullable CakeResponse cakeResponse) {
cakes = new ArrayList<>();
if(cakeResponse != null) {
CakeResponseCakes[] cakeList = cakeResponse.getCakes();
if(cakeList != null) {
for(CakeResponseCakes caky: cakeList){
CakeResponseCakes cake = new CakeResponseCakes();
cake.setId(caky.getId());
cake.setDetailDescription(caky.getDetailDescription());
cake.setPreviewDescription(caky.getPreviewDescription());
cake.setTitle(caky.getTitle());
cake.setImage(caky.getImage());
cakes.add(cake);
}

}
}
adapter.addCakes(cakes);
adapter.notifyDataSetChanged();
}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
}

private void initializeList() {
recyclerView.hasFixedSize();
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapter = new CakesAdapter(getLayoutInflater());
recyclerView.setAdapter(adapter);
}

这是我的 logCat:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.athenaishere.cakemvvm/ir.athenaishere.cakemvvm.activities.base.CakesActivity}: java.lang.RuntimeException: Cannot create an instance of class ir.athenaishere.cakemvvm.activities.base.mvvm.CakeViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
at android.app.ActivityThread.access$900(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5753)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Caused by: java.lang.RuntimeException: Cannot create an instance of class ir.athenaishere.cakemvvm.activities.base.mvvm.CakeViewModel
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:201)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)
at ir.athenaishere.cakemvvm.activities.base.CakesActivity.onCreate(CakesActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6112)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
at android.app.ActivityThread.access$900(ActivityThread.java:168) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5753) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
Caused by: java.lang.NoSuchMethodException: <init> [class android.app.Application]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getConstructor(Class.java:495)
at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) 
at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) 
at ir.athenaishere.cakemvvm.activities.base.CakesActivity.onCreate(CakesActivity.java:51) 
at android.app.Activity.performCreate(Activity.java:6112) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
at android.app.ActivityThread.access$900(ActivityThread.java:168) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5753) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

更新:这是我的模块类:

@Module
public class CakeModule {
private CakesActivity cakesActivity;

public CakeModule(CakesActivity cakesActivity) {
this.cakesActivity = cakesActivity;
}

@CakeContext
@BaseScope
@Provides
public CakesActivity providesCakeActivity() {
return this.cakesActivity;
}

@BaseScope
@Provides
public Model providesModel(NetworkService networkService) {
return new Model(networkService);
}

@BaseScope
@Provides
CakeViewModelFactory provideViewModel(Model model) {
return new CakeViewModelFactory(model, cakesActivity.getApplication());
}

}

networkService 是我的 API 所在的接口(interface)。我的模型和应用程序在这个模块类中提供。

如有任何帮助,我们将不胜感激。 3>

最佳答案

我不确定,但是,在您的“CakeViewModelFactory”中,您正在执行构造函数注入(inject),其中您正在注入(inject)模型和应用程序的实例,但是您的模块类没有提供应用程序上下文的@Provides 方法。所以可能将此添加到您的 Module 类中会有所帮助:

@Provides
public Context provideApplicationContext(Application application) {
return applicaton;
}

并且还尝试在调用 CakesActivity 类中的 super.onCreate() 之前注入(inject)依赖项。

如果有任何解决方案解决了您的问题,请告诉我。 :)

关于android - ViewModelFactory can't create an instance 错误创建自定义ViewModelFactory类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615322/

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