9

Please someone help me! I am going crazy, this should work. I have the following error message when I try to build my Android project:

Android resource linking failed
/Users/slehrbaum/StudioProjects/OneNightComps/Android/app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/fragment_login.xml:17: error: attribute errorText (aka lehrbaum.de.onenightcomps:errorText) not found.
error: failed linking file resources.

the error message does mention the errorText attribute. I use the errorText attribute in the xml this way (full xml here):

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/usernameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username"
        app:hintEnabled="true"
        app:errorEnabled="true"
        app:errorText="Hi"
        >
        <!--app:errorText="Please provide a username."-->
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="username"
            android:inputType="text"
            android:text="@={viewModel.username}"
            />
    </com.google.android.material.textfield.TextInputLayout>

This is the way I have defined errorText in my Kotlin file (full file here):

object ViewDataBindingExtensions {
    @JvmStatic
    @BindingAdapter("errorText")
    fun bindErrorText(textInputLayout: TextInputLayout, errorText: String) {
        textInputLayout.error = errorText
    }
}

I just don't understand why this happens. Is there some sort of import that I can put in the layout file saying where the BindingAdapter is? Do I have some wrong with my Gradle files? I compared it to the GitHub project in this question which apparently got solved and I do not see the difference to my project. According to the answer I should add the Kotlin-kapt plugin to my Gradle build, which I did. I also looked through the rest of the project and compared. To no avail. You can find my whole build.gradle file here as well as the rest of the project.

Please help me!

findusl
  • 2,454
  • 8
  • 32
  • 51
  • Have you tried to add `kapt "com.android.databinding:compiler:$gradleVersion"`? – Anton Holovin Feb 25 '19 at 23:00
  • @AntonHolovin I tried now, but it didn't help. I assume I have to add it to the build.gradle of the app project? And the version should be the same as the one of 'com.android.tools.build:gradle:' right? That didn't work for me. But maybe I did something wrong. I pushed it to GitHub you can find it under the link https://github.com/findusl/OneNightComps/blob/feature/register/Android/app/build.gradle – findusl Feb 26 '19 at 08:11

3 Answers3

44

The problem is connected with the way you pass String value to app:errorText.

Use @{``} to pass this value.

Fixed part of fragment_login.xml:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/username"
    app:hintEnabled="true"
    app:errorText="@{`Please provide a username.`}"
    app:errorEnabled="@{!viewModel.usernameValid}">

Having apply plugin: 'kotlin-kapt' in app/build.gradle is mandatory.

Anton Holovin
  • 5,513
  • 1
  • 31
  • 33
  • 1
    You madman. Thank you so much for the idea that the string literal is the problem. I don't understand why I can't pass a string literal, but I don't care since string literals aren't good anyway. I just had it there because I thought it would be easier to test. When I referenced a variable of the viewmodel it finally compiled and worked. I'll mark your answer as correct, you might want to update it for others, your exact solution gives an error `data binding error ****msg:Syntax error: no viable alternative at input ''` – findusl Feb 26 '19 at 20:55
0

Try using

fun bindErrorText(textInputEditText: TextInputEditText, errorText: String) {
 textInputEditText.error = errorText }
Vahalaru
  • 380
  • 4
  • 15
  • Sadly this doesn't solve the problem. The error message is now on a different line because the TextInputEditText is on a different line but the rest is the same. – findusl Feb 26 '19 at 17:52
0

Another reason which is not often discussed is you need to have your layout inside "" tag, else databinding doesn't work. I spent an hour for this.

nayakasu
  • 889
  • 1
  • 11
  • 32