我的库模块中有一个抽象的 BaseActivity:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Normal method to inflate the layout
mBinding = DataBindingUtil.setContentView(this, R.layout.base_view_stub_layout);
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
if (mBinding != null && mBinding.viewStub != null && mBinding.viewStub.getViewStub() != null) {
mBinding.viewStub.getViewStub().setLayoutResource(getLayoutId());
}
if (!hasStubInflated) {
View inflatedView = mBinding.viewStub.getViewStub().inflate();
//Abstract method
onViewStubInflated(inflatedView, savedInstanceState);
//Normal method to hide progress bar
onViewStubInflated();
}
initControllers();
handleViews();
setListeners();
restoreValues(savedInstanceState);
}
AppModule 中的 MainActivity 扩展了库模块的 BaseActivity,因此获得了一个抽象方法来覆盖:
MainActivity 扩展 BaseActivity
private ActivityMainBinding mBinding;
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
@Override
public void onViewStubInflated(View inflatedView, Bundle savedInstanceState) {
if (mBinding == null || mBinding.getRoot() != inflatedView) {
mBinding = ActivityMainBinding.bind(inflatedView);
}
}
当我运行该应用程序时,我得到:
Caused by: java.lang.RuntimeException: view must have a tag
at com.example.android.emailapp.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:67)
at androidx.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
at androidx.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)
at androidx.databinding.ViewDataBinding.bind(ViewDataBinding.java:693)
at com.example.android.emailapp.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:99)
at com.example.android.emailapp.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:87)
at com.example.android.emailapp.login.MainActivity.onViewStubInflated(MainActivity.java:86)
at com.library.android.common.ui.baseui.BaseActivity.onCreate(BaseActivity.java:52)
请注意,我在 BaseActivity 和 MainActivity 的 xml 中都有布局标记。
我试过这个没有成功。
mBinding.viewStub.getViewStub ().setLayoutResource(getLayoutId())是 BaseActivity 中的一个问题,因为它会尝试从 AppModule 获取布局?如果是这样,我该如何解决?
如果您知道答案,请尝试解释为什么会发生这种情况以及解决方案...
已经试过了