gpt4 book ai didi

android - View 必须有标签

转载 作者:行者123 更新时间:2023-12-04 11:45:39 25 4
gpt4 key购买 nike

我的库模块中有一个抽象的 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 中都有布局标记。

我试过 this没有成功。

mBinding.viewStub.getViewStub().setLayoutResource(getLayoutId()) 是 BaseActivity 中的一个问题,因为它会尝试从 AppModule 获取布局? 如果是这样,我该如何解决?

如果您知道答案,请尝试解释为什么会发生这种情况以及解决方案...

已经尝试过

View must have a tag error

What does it mean by view must have a tag error

How to use databinding for viewstub

最佳答案

如果您使用自定义类作为 your_layout.xml 的 Root View 那么你必须覆盖 View 的所有构造函数

你的类

class MyCustomView : FrameLayout {
constructor(context: Context) : super(context){
...
}

constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
...
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr){
...
}
}

你的布局
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="viewModel" type="......" />
</data>

<MyCustomView
android:layout_width="match_parent"
android:layout_height="match_parent">

</MyCustomView>

</layout>

关于android - View 必须有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681642/

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