“由:java.lang.RuntimeException:view引起的实际含义必须具有标签”是什么?

Et *_*Crc 8 java android view android-activity

如果知道想要什么标签,请通知我。

Caused by: java.lang.RuntimeException: view must have a tag

__BaseActivity.java

    @Override
    public void setContentView(int layoutResID) {

        mBinding.contentParent.removeAllViews();
        DataBindingUtil.inflate(LayoutInflater.from(this), layoutResID, mBinding.contentParent, true);
        super.setContentView(mBinding.getRoot());
    }
Run Code Online (Sandbox Code Playgroud)

__ChildActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.my_wallet);
}
Run Code Online (Sandbox Code Playgroud)

错误logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydev}: java.lang.RuntimeException: view must have a tag
        at <more...>
     Caused by: java.lang.RuntimeException: view must have a tag
        at android.databinding.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:121)
Run Code Online (Sandbox Code Playgroud)

小智 11

当试图用于DataBindingUtil.inflate()膨胀不支持数据绑定的布局时,通常会发生这种情况。换句话说,您要膨胀的布局没有其根元素为<layout>

当重构一个活动以使用数据绑定时,我遇到了这个问题,并且该活动具有多种布局。我成功地重构了其中一种布局以在<layout>元素的根部包括元素,但是我没有重构所有其他布局(其他屏幕密度,语言,模块等的布局)。

检查并确保将所有可能的匹配布局都配置<layout>为其根元素。

请参阅此开发人员文档布局和绑定表达式


raj*_*ath 5

由于库的布局文件(标记了错误的文件)与应用程序模块中的另一个文件同名,因此发生了这种情况。两者都使用数据绑定。


小智 5

发生此错误的另一个场景是在 RecyclerView 的 ViewHolder 中。

避免在 ViewHolder 的绑定方法中初始化绑定实例

class BindingAdapter(private val items: List<Any>): RecyclerView.Adapter<BindingHolder>() {
      override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder {}

      override fun onBindViewHolder(holder: BindingHolder, position: Int) {
           holder.bindItem(items[position])
      }
 }

class BindingHolder(view: View): RecyclerView.ViewHolder(view) {
    fun bindItem(item: Any) {
        //Don't do this
        val binding = ItemSampleBinding.bind(itemView)
    }
}
Run Code Online (Sandbox Code Playgroud)

数据绑定实例应该在 bind 方法之外初始化,因为 ViewHolders 可以被回收,并且在上面的代码中,我们可以尝试从已经绑定的视图创建绑定实例。

而是在 ViewHolder 的初始化块中创建绑定实例(这可以在init{}块中或在类声明之后,如下所示)

class BindingHolder(view: View): RecyclerView.ViewHolder(view) {
    val binding = ItemSampleBinding.bind(view)

    fun bindItem(item: Any) {
        //Rest of ViewHolder logic
        //binding.textView.text = "Something nice"
    }
}
Run Code Online (Sandbox Code Playgroud)


Et *_*Crc 4

不知道,但工作。

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.my_wallet, null, false);
            setContentView(mBinding.getRoot());
    }
Run Code Online (Sandbox Code Playgroud)

或者

如果您的根布局必须与高/宽的 match_parrent 匹配。就像https://github.com/umano/AndroidSlidingUpPanel

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout._activity_layout, null, false);

        // TODO resolve this concurrent assignment
        // tricky method because sliding layout must be as parent / high is HIGH_EXACT to MATCH_PARENT 
        setContentView(mBinding.getRoot(), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

}
Run Code Online (Sandbox Code Playgroud)