问题描述
我正在实现 MVVM 并使用 viewpager2,并在片段上附加了 tablayout
我为 tablayout 的每个片段都有一个 viewmodel 类,我收到了这个运行时错误
"无法使用viewpager创建类viewmodel的实例
这是我的视图模型类:
public class Cranesviewmodel extends Androidviewmodel {
private IndustrialCranesRepository repository;
private LiveData<List<TrackingModel>> allIndustrialTrackingItems;
public Cranesviewmodel(@NonNull Application application) {
super(application);
repository = new IndustrialCranesRepository(application);
allIndustrialTrackingItems = repository.getAllIndustrialData();
}
public LiveData<List<TrackingModel>> getAllIndustrialTrackingItems() {
return allIndustrialTrackingItems;
}
public class Constructionviewmodel extends Androidviewmodel {
private ConstructionPortMaritimeRepository repository;
private LiveData<List<ConstructionModel>> allConstructionTrackingItems;
public Constructionviewmodel(@NonNull Application application) {
super(application);
repository = new ConstructionPortMaritimeRepository(application);
allConstructionTrackingItems = repository.getAllConstructionData();
}
public LiveData<List<ConstructionModel>> getAllConstructionTrackingItems()
{
return allConstructionTrackingItems;
}
}
片段代码:
@Override
public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
viewmodel = new viewmodelProvider(requireActivity()).get(Constructionviewmodel.class);
viewmodel.getAllConstructionTrackingItems().observe(requireActivity(),new Observer<List<ConstructionModel>>() {
@Override
public void onChanged(List<ConstructionModel> constructionModels) {
ConstructionAdapter adapter = new ConstructionAdapter(constructionModels,ConstructionFragment.this);
constructionRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
为什么我会收到此错误?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)