简体   繁体   English

视图必须有标签

[英]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是什么意思

How to use databinding for viewstub 如何为viewstub使用数据绑定

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的根视图,那么您必须覆盖视图的所有构造函数

your class你的 class

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){
        ...
    }
}

your layout你的布局

<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.

相关问题 查看android数据绑定一定有标签错误 - View must have a tag error in android data binding `Caused by: java.lang.RuntimeException: view must have a tag`的实际含义是什么? - What the actual meaning `Caused by: java.lang.RuntimeException: view must have a tag`? 按钮错误的选择器“有效的XML文档必须具有根标记” - Selector for button error “Valid XML document must have a root tag” 自定义视图中的ViewStub返回'ViewStub必须具有有效的layoutResource' - ViewStub inside custom view returning 'ViewStub must have a valid layoutResource' 膨胀类片段时出错。 必须指定唯一的android:id,android:tag,或者具有id的父级 - Error inflating class fragment. Must specify unique android:id, android:tag, or have a parent with an id 顶级元素未完成,并且有效XML必须具有根标记:Android Studio - Top level element is not completed and Valid XML must have a root tag : Android Studio 为什么我有这个错误:view.findViewById<mapview> (R.id.map) 不得为 null</mapview> - why i have this error: view.findViewById<MapView>(R.id.map) must not be null 我正在开发一个可以上传文件的应用,安装此应用的人必须能够下载和查看这些文件 - I am developing an app to upload files and people who have installed this app must be able to download and view these files 我必须获得许可吗? - Must I have licensing? 引起原因:java.lang.IllegalArgumentException:二进制XML文件第9行:必须指定唯一的android:id,android:tag或具有ID的父对象 - Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Must specify unique android:id, android:tag, or have a parent with an id for
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM