gpt4 book ai didi

java - ViewModel 没有零参数构造函数错误,原因是 ViewModelFactory 一直为 null(不是 100% 确定)

转载 作者:行者123 更新时间:2023-11-30 01:54:19 25 4
gpt4 key购买 nike

到目前为止,我遵循 Android 开发人员指南了解架构组件,当然,问题是他们的示例无法与默认工厂一起使用,原因是“无零参数异常”。我遵循了 Google 示例存储库上的一些逻辑,以及相当多的 StackOverflow 建议答案、博客和教程。但我仍然遇到同样的错误。

现在,我想我已经找到了一个问题,似乎我的 ViewModelFactory 一直为 null,因此 ViewModelProvider 将其替换为默认 Factory,当然默认 Factory 需要一个零参数构造函数。但我仍然不知道代码到底出了什么问题。

所以我基本上有一个 ViewModelFactory

public class ViewModelFactory implements ViewModelProvider.Factory {

private final Map<Class<? extends CustomViewModel>, Provider<CustomViewModel>> viewModels;

@Inject
public ViewModelFactory(Map<Class<? extends CustomViewModel>, Provider<CustomViewModel>> viewModels) {
this.viewModels = viewModels;
}

@SuppressWarnings("unchecked")
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {

Provider<? extends CustomViewModel> viewModelProvider = viewModels.get(modelClass);

if (viewModelProvider == null) {

for (Map.Entry<Class<? extends CustomViewModel>, Provider<CustomViewModel>> entry : viewModels.entrySet()) {

if (modelClass.isAssignableFrom(entry.getKey())) {

viewModelProvider = entry.getValue();
break;
}
}
}

if (viewModelProvider == null) {

throw new IllegalArgumentException("unknown model class " + modelClass);
}

try {

return (T) viewModelProvider.get();
} catch (Exception e) {

throw new RuntimeException(e);
}
}
}

和AppComponent

@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, ViewModelModule.class})
public interface AppComponent {

@Component.Builder
interface Builder {

@BindsInstance
Builder application(App application);
AppComponent build();
}
void inject(App application);
}

然后是ViewModelModule

@Module
public abstract class ViewModelModule {

@Binds
abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory viewModelFactory);

@Binds
@IntoMap
@ViewModelKey(HourlyViewModel.class)
abstract ViewModel bindHourlyViewModel(HourlyViewModel hourlyViewModel);
}

还有一个 ViewModelKey

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewModelKey {

Class<? extends CustomViewModel> value();
}

还有一个 ViewModel

public class HourlyViewModel extends CustomViewModel {

private LiveData<HourlyData> hourlyData;
private MainRepository mainRepo;

@Inject
public HourlyViewModel(HourlyRepository mainRepo) {

this.mainRepo = mainRepo;
}

@Override
public void init(int id, long start, long stop) {
super.init(id, start, stop);

// some logic
}
}

还有一个额外的 CustomViewModel

public class CustomViewModel extends ViewModel {

public OpenDataApi api;

public int id;
public long start;
public long stop;

/**
*
* @param id
* @param start
* @param stop
*/
public void init(int id, long start, long stop) {

api = OpenDataApi.getRetrofitInstance().create(OpenDataApi.class);
this.id = id;
this.start = start;
this.stop = stop;
}
}

我的申请

public class App extends Application {

@Override
public void onCreate() {
super.onCreate();

DaggerAppComponent
.builder()
.application(this)
.build()
.inject(this);

AndroidThreeTen.init(this);
}
}

以及我如何尝试在 Fragment 中调用 ViewModel

public class FragmentHourly extends Fragment {

@Inject
ViewModelFactory viewModelFactory;

private HourlyViewModel viewModel;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

viewModel = ViewModelProviders.of(this, viewModelFactory).get(HourlyViewModel.class);
}
}

我到底错过了什么?我的实现是否错误,或者缺少什么?

干杯并感谢您的帮助

最佳答案

我今天也遇到了类似的拦截器,错误是 fragment 中遗漏了以下代码:

`

@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}

`

关于java - ViewModel 没有零参数构造函数错误,原因是 ViewModelFactory 一直为 null(不是 100% 确定),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54993515/

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