Android Bindings Stopped working
Asked Answered
P

4

8

Just suddenly my bindings for android stoped working, anything I build now, I just get this message.

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String on android.widget.EditText. file:C:\path\to\layout\layout.xml loc:85:12 - 96:54 ****\ data binding error ****

What I have tried

First it was suggested the bindings won't compile if there are errors in my files, so removed all layout files where I used bindings up to one file layout.xml. There I have

 <EditText
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:text="@{model.name}" />

... and it works well, however if i add the two-way binding android:text="@={model.name}" It throws the previous error.

Next, I add

@InverseBindingAdapter(attribute = "android:text")
public static String captureEditTextValue(EditText view) {
    return view.getText().toString();
}

...then it throws new error.

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.

java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Could not find event 'android:textAttrChanged' on View type 'android.widget.EditText' file:C:\Users\EdgeTech\AndroidStudioProjects\purse\purse-customer\app\src\main\res\layout\get_phone_layout.xml loc:85:12 - 96:54 ****\ data binding error ****

Went further, to refactor to this

 @InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
 public static String captureEditTextValue(EditText view) {
        return view.getText().toString();
 }

...still gives previous error.

My Setup

  • Android Studio: 2.3.3
  • Gradle Build Tools: 2.3.3
Parthinia answered 22/9, 2017 at 1:10 Comment(7)
When it comes to two-way binding, android:text="@={model.name}" this line is enough if your variable is String, If you have other datatypes then you need to do inverse binding.Pesek
@RaviRupareliya yes its' a string.Parthinia
Then as i already told,if it is String, InverseBinding is not at all needed.Pesek
@RaviRupareliya, okay but its still doesn't build regardlessParthinia
Can you post the rest of the XML and the code in your model? That would help. If you have a short project that demonstrate this problem, that would be ideal.Curly
Can you post your whole XML file and your whole model file?Tael
I am having the same issue on Android Preview since Friday. Code that used to work stopped working on a simple String bind on an EditText. Marked answer does not fix it.Hiatus
E
3

Two way binding needs this type:

ObservableField<T>

for example: in viewModel.class

public ObservableField<String> productName = new ObservableField<>();

in layout.xml:

<EditText
       android:layout_width="wrap_content"
       android:layout_height="match_parent"
       android:text="@={viewModel.productName}" />
Endor answered 28/9, 2017 at 9:46 Comment(0)
T
2

Get rid of the InverseBindingAdapter as I do not believe it is necessary for a String.

Then, in your EditText XML tag, change android:text="@={model.name} --> android:text="@={`` + model.name}".

Trodden answered 27/9, 2017 at 11:54 Comment(1)
Please let me know if this does not fix your issue. I see you are running on Android 2.3+ (the minimum support for the bug fix) so this solution could very well solve the problem.Trodden
T
2

You should just be able to change:

@InverseBindingAdapter(attribute = "android:text")
public static String captureEditTextValue(EditText view) {
    return view.getText().toString();
}

to:

@InverseBindingAdapter(attribute = "android:text")
public static String getText(TextView view) {
    return view.getText().toString();
}
Tael answered 28/9, 2017 at 0:25 Comment(0)
P
1

Thanks,

However it's working now. It started working back after I upgraded my Build Tools and SDK Platform tools to version 26. Not still sure why it was breaking with version 25 though.

I tried all the solutions provided, all to no avail, upgrading it, was the only solution that worked for me.

Parthinia answered 1/10, 2017 at 23:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.