2

Trying to use DataBinding in a CustomView(Group)

layout looks like this:

<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<merge
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/customview_checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="@string/checkbox_value_key"
        android:text="My CheckBox"/>
    ....
</merge>
</layout>

CustomView (extends FrameLayout) inflation:

view = DataBindingUtil.inflate((LayoutInflater) getContext().getSystemService(Context
            .LAYOUT_INFLATER_SERVICE), R.layout.custom_view, this, true);

results in this error:

Error:(46, 61) error: cannot find symbol method getResources()

Generated Binding Class:

public ViewBodyPartsBinding(android.databinding.DataBindingComponent bindingComponent, View[] root) {
    super(bindingComponent, root[0], 0);
    final Object[] bindings = mapBindings(bindingComponent, root, 9, sIncludes, sViewsWithIds);
    this.mboundView0 = (android.widget.ImageView) bindings[0];
    this.mboundView0.setTag(null);
    this.mboundView2 = (android.support.percent.PercentRelativeLayout) bindings[2];
    this.mboundView2.setTag(null);
    ....
    this.customviewCheckbox1 = (android.widget.CheckBox) bindings[1];
    this.customviewCheckbox1.setTag(root.getResources().getString(<package>.R.string.checkbox_value_key));
    ....
    setRootTag(root);
    // listeners
    invalidateAll();
}
cwiesner
  • 934
  • 1
  • 8
  • 24
  • i do not set the tag on the element. therefore it should work imho – cwiesner Oct 06 '16 at 08:50
  • This seems to be a problem with tag attribute that you use for CheckBox. I came up with this http://stackoverflow.com/questions/31826201/android-data-binding-not-working-with-view-androidtag-property after trying out myself and searching – Raghunandan Oct 06 '16 at 09:17
  • George Mount replied to you with `or supply a fixed tag or resource string` . I'm using a resource string that's why i assume it should work. – cwiesner Oct 06 '16 at 10:55
  • did you try removing the tag attribute. You can check yourself – Raghunandan Oct 06 '16 at 13:33
  • This looks like a bug in our code generator. Is this in Android Studio 2.2 or 2.1? – George Mount Oct 07 '16 at 21:22
  • 4
    This bug looks like it still exists. Until it is fixed for Android Studio 2.3, you can work around it by using a hard-coded string. Sorry! – George Mount Oct 07 '16 at 21:47
  • Possible duplicate of [Android data binding is not working with attributes](http://stackoverflow.com/questions/35590608/android-data-binding-is-not-working-with-merge-attributes) – tynn Oct 09 '16 at 11:12

1 Answers1

0

Bug identified according to George Mount. Waiting for Android Studio 2.3 to fix that

Community
  • 1
  • 1
cwiesner
  • 934
  • 1
  • 8
  • 24