View must have a tag error in android data binding

前端 未结 7 2305
清酒与你
清酒与你 2021-02-13 05:54

I\'ve two layouts for a screen. Activity works fine while setting a layout for Mobile device but it\'s causing error while setting layout for tablet device. The main issue is:

相关标签:
7条回答
  • 2021-02-13 06:05

    I was having this problem when using an array adapter, having a crash due to a missing tag on convertView. In my getView(), I was doing:

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        if (convertView == null) {
            DataBindingUtil.inflate<ItemSpinnerDropDownWorkPackageFilterBinding>(
                LayoutInflater.from(parent.context),
                R.layout.item_spinner_drop_down_work_package_filter,
                parent,
                false
            )
        } else {
            binding = ItemSpinnerDropDownWorkPackageFilterBinding.bind(convertView) 
            binding.text1.setText(getItem(position))
            setDividerVisibility(binding.divider, position)
            return convertView
        }    
    
    }
    

    Which was crashing. The solution was to set the tag on the first run through getView:

     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val binding = if (convertView == null) {
            DataBindingUtil.inflate<ItemSpinnerDropDownWorkPackageFilterBinding>(
                LayoutInflater.from(parent.context),
                R.layout.item_spinner_drop_down_work_package_filter,
                parent,
                false
            )
        } else {
            convertView.tag as ItemSpinnerDropDownWorkPackageFilterBinding
        }
        binding.text1.setText(getItem(position))
        setDividerVisibility(binding.divider, position)
        binding.root.tag = binding
        return binding.root
    
    }
    
    0 讨论(0)
  • 2021-02-13 06:16

    You need to add layout tag at start of your app_toolbar_layout layout file

    0 讨论(0)
  • 2021-02-13 06:20

    You must have <layout> tag into your all XML views (portrait, landscape, tablet, etc.). Even you have to include <layout> tag into included views ("@layout/app_toolbar_layout").

    0 讨论(0)
  • 2021-02-13 06:21

    I just needed to clean and rebuild after deleting a resource file.

    0 讨论(0)
  • 2021-02-13 06:26

    Keeping two layouts, one with layout tag of data-binding and another without it, is the common cause of this issue.

    I got stuck when I renamed my two normal layout files with same name (/layout and /layout-sw720dp) and used tag. Then, it worked for mobile device but not for tablet. So, after cleaning project, it all started working.

    0 讨论(0)
  • 2021-02-13 06:26

    I ran into this when I had:

    • A library module defining a layout resource
    • An app module that depended upon that library module defining the same layout resource
    • The library layout resource was set up for data binding (e.g., root <layout> element), but the app module's edition of that layout resource was not

    In my case, the app module's layout was left over from when I created the project. Removing it cleared up the problem.

    0 讨论(0)
提交回复
热议问题