如何解决无法使用 viewpager 和 tablayout 创建类 viewmodel 的实例
我正在实现 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 举报,一经查实,本站将立刻删除。