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

不打扰是莪最后的温柔 提交于 2019-12-09 02:20:16

问题


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

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

回答1:


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:

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

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



回答2:


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




回答3:


Step 1: create 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:

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

Step 3: bind custom view to your switcher like this:

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

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


来源:https://stackoverflow.com/questions/41609252/android-data-binding-error119-29-identifiers-must-have-user-defined-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!