[英]View must have a tag
I have an abstract BaseActivity in my library module:我的库模块中有一个抽象的 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);
}
The MainActivity from AppModule extends the BaseActivity of library module and hence, getting an abstract method to override: AppModule 中的 MainActivity 扩展了库模块的 BaseActivity,因此获得了一个抽象方法来覆盖:
MainActivity extends 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);
}
}
When I run the app, I am getting:当我运行该应用程序时,我得到:
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)
Note that I have layout tag in both xml of BaseActivity and MainActivity.请注意,我在 BaseActivity 和 MainActivity 的 xml 中都有布局标签。
I have tried this with no success.我试过这个没有成功。
Is mBinding.viewStub.getViewStub().setLayoutResource(getLayoutId()) an issue in BaseActivity as it will try to get the layout from AppModule? mBinding.viewStub.getViewStub().setLayoutResource(getLayoutId())是 BaseActivity 中的一个问题,因为它会尝试从 AppModule 获取布局? if it is that, how can I solve it?
如果是这样,我该如何解决?
If you know the answer, please try to explain why this is happening along with the solution...如果您知道答案,请尝试解释为什么会发生这种情况以及解决方案...
Already tried已经试过了
View must have a tag error 视图必须有标签错误
What does it mean by view must have a tag error view must have a tag error是什么意思
If you are using the custom class as the root view of your_layout.xml
then you must override all constructors of view如果您使用自定义 class 作为
your_layout.xml
的根视图,那么您必须覆盖视图的所有构造函数
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>
You must wrap your viewStub's layouts with <layout></layout>
as well, which is what they are expecting: Either using DataBinding everywhere, or simply nowhere.您还必须使用
<layout></layout>
来包装 viewStub 的布局,这正是他们所期望的:要么在任何地方使用 DataBinding,要么根本不在任何地方。
If you don't want DataBinding on the viewStub, then extra work needs to be done.如果您不想在 viewStub 上使用 DataBinding,则需要完成额外的工作。
For example if itemData
is the DataBindingImpl object for my listview, and I am to inflate some sort of more options
overlay-ViewStub, then I need to store the inflated view myself, and avoid accessing the `mRoot field of the DataBinding's ViewStubProxy例如,如果
itemData
是我的列表视图的 DataBindingImpl object,并且我要膨胀某种more options
overlay-ViewStub,那么我需要自己存储膨胀的视图,并避免访问 DataBinding 的 ViewStubProxy 的 `mRoot 字段
ViewGroup inflated = (ViewGroup) itemData.someView.getTag();
if(inflated==null) {
ViewStub stub = itemData.viewStubProxy.getViewStub();
if(stub!=null) {
stub.setOnInflateListener(null);
inflated = (ViewGroup) stub.inflate();
// what I need is simply to set onClickListener on all of it's children. (one for-loop inside)
Utils.setOnClickListenersOneDepth(inflated, onClickListener, 3, null);
itemData.someView.setTag(inflated);
}
}
if(inflated!=null) {
// what I need then is to animate the overlay.
View mRoot = inflated;
int width = itemData.getRoot().getWidth();
mRoot.setAlpha(0);
mRoot.setTranslationX(width/3);
mRoot.animate().alpha(1);
mRoot.animate().translationX(0);
}
Never invoke itemData.viewStubProxy.getViewStub().getRoot()
、 .isInflated()
or so because we are now storing the inflated
outside of the DataBindingImpl object.永远不要调用
itemData.viewStubProxy.getViewStub().getRoot()
、 .isInflated()
左右,因为我们现在将inflated
存储在 DataBindingImpl object 之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.