简体   繁体   English

Android数据绑定 - 错误:(119,29)标识符必须具有XML文件中的用户定义类型。 main_radio_subscribe缺少它

[英]Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing it

I been trying to control the visibility of a view using the Implicit Attribute Listeners( reference ) in android data binding which allows to access views by id and access attributes like checked, visible etc ..., however when trying to use this, it throws an error like so 我一直试图使用android数据绑定中的隐式属性监听器( 引用 )来控制视图的可见性,它允许通过id和访问属性(如checked,visible等)访问视图,但是当尝试使用它时,它会抛出像这样的错误

Error:(119, 29) Identifiers must have user defined types from the XML file. addTodo_switch_remind is missing it
<android.support.v7.widget.SwitchCompat
        android:id="@+id/addTodo_switch_remind"
        style="@style/MediumTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addTodo_space_project"
        android:text="@string/add_todo_remind_label"
        android:textOff="@string/generic_no_text"
        android:textOn="@string/generic_yes_text" />

    <android.support.v4.widget.Space
        android:id="@+id/addTodo_space_remind"
        style="@style/FormsSpacingStyle"
        android:layout_below="@+id/addTodo_switch_remind" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/addTodo_space_remind"
        android:orientation="vertical"
        android:padding="@dimen/grid_box_single"
        android:visibility="@{addTodo_switch_remind.checked ? View.VISIBLE : View.GONE}">

As you use View.VISIBLE / View.GONE in your .xml file, you should import the View type by doing adding <import type="android.view.View"/> in the data section like the following: 当您在.xml文件中使用View.VISIBLE / View.GONE时,您应该通过在数据部分中添加<import type="android.view.View"/>来导入View类型,如下所示:

<data>
    <import type="android.view.View"/>

    <variable
        name="viewModel"
        type="xx.xx.MyViewModel"/>
</data>

Looks like the implicit Attribute listeners use camel case when it is used in the expressions, thanks to this post I figured it out. 看起来隐式属性侦听器在表达式中使用驼峰的情况下,感谢这篇文章我想出来了。

<!--Recurring Reminder -->
        <android.support.v7.widget.SwitchCompat
            android:id="@+id/addTodo_switch_remind"
            style="@style/MediumTextViewStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addTodo_space_project"
            android:text="@string/add_todo_remind_label"
            android:textOff="@string/generic_no_text"
            android:textOn="@string/generic_yes_text" />

        <android.support.v4.widget.Space
            android:id="@+id/addTodo_space_remind"
            style="@style/FormsSpacingStyle"
            android:layout_below="@+id/addTodo_switch_remind" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/addTodo_space_remind"
            android:orientation="vertical"
            android:padding="@dimen/grid_box_single"
            android:visibility="@{addTodoSwitchRemind.checked ? View.VISIBLE : View.GONE}">

Documenting for others who have the same issue 记录具有相同问题的其他人

Step 1 : create BindingAdapter : 第1步 :创建BindingAdapter

@BindingAdapter("android:visibility")
public static void setVisibility(final View view, @IdRes int layourId) {
    SwitchCompat switcher = (SwitchCompat)view.getRootView().findViewById(layourId)
    switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            view.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        }
    }
}

Step 2 : import R class in databinding data section at you layout.xml : 第2步 :在layout.xml中的数据绑定数据部分导入R类:

<data>
     <import type="example.package.R"/>
</data>

Step 3 : bind custom view to your switcher like this: 第3步 :将自定义视图绑定到切换台,如下所示:

<android.support.v7.widget.SwitchCompat
    android:id="@+id/addTodo_switch_remind"/>

<LinearLayout
    android:visibility="@{R.id.addTodo_switch_remind">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据绑定未获得“标识符必须具有XML文件中的用户定义类型”错误 - data binding not getting “Identifiers must have user defined types from the XML file” error 标识符必须具有XML文件中的用户定义类型,并带有observablefield的数据绑定 - Identifiers must have user defined types from the XML file, databinding with observablefield 错误:公用类型TodoListItemView必须在其自己的文件中定义。 安卓 日食 - Error: The public type TodoListItemView must be defined in its own file. Android. Eclipse 查看android数据绑定一定有标签错误 - View must have a tag error in android data binding XML文件错误。 Android编程 - Error in XML file. Android programming 有一些麻烦与xml文件。 Android系统。 日食。 错误:无效的开始标记PreferenceScreen - Have some troubles vith xml file. Android. Eclipse. error: Invalid start tag PreferenceScreen android.view.InflateException: Binary XML file line #119: Error inflating class com.google.android.material.textview.MaterialTextView - android.view.InflateException: Binary XML file line #119: Error inflating class com.google.android.material.textview.MaterialTextView “错误膨胀类菜单”-android.view.InflateException:二进制XML文件行#119:错误膨胀类菜单 - “Error inflating class menu” - android.view.InflateException: Binary XML file line #119: Error inflating class menu Android数据绑定XML错误 - Android Data Binding XML Error 双向数据绑定:视图缺少用户定义的类型 - Two-Way Data Binding: View is missing user defined type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM