【问题标题】:Android Data binding - Error:(119, 29) Identifiers must have user defined types from the XML file. main_radio_subscribe is missing itAndroid 数据绑定 - 错误:(119, 29) 标识符必须具有来自 XML 文件的用户定义类型。 main_radio_subscribe 缺少它
【发布时间】:2017-01-12 09:21:02
【问题描述】:

我一直在尝试使用 android 数据绑定中的隐式属性侦听器 (reference) 来控制视图的可见性,它允许通过 id 访问视图并访问已检查、可见等属性...,但是当尝试使用它,它会抛出这样的错误

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}">

【问题讨论】:

  • 发布您正在使用的 xml 和 BindingAdapter

标签: android android-databinding


【解决方案1】:

当您在 .xml 文件中使用 View.VISIBLE / View.GONE 时,您应该通过在数据部分添加 &lt;import type="android.view.View"/&gt; 来导入 View 类型,如下所示: p>

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

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

【讨论】:

    【解决方案2】:

    看起来隐式属性侦听器在表达式中使用时使用驼峰式大小写,感谢post 我想通了。

    <!--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}">
    

    为有相同问题的其他人记录

    【讨论】:

    • addTodoSwitchRemind 应该是您的模型视图类的名称。
    • @Blackbelt - 不,那是 appcompact 复选框的 id
    【解决方案3】:

    第一步:创建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);
            }
        }
    }
    

    第 2 步:在您的数据绑定数据部分中导入Rlayout.xml

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

    第 3 步:将自定义视图绑定到您的切换器,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      相关资源
      最近更新 更多