【问题标题】:Android Binding Adapter is not found未找到 Android 绑定适配器
【发布时间】:2019-07-19 09:58:30
【问题描述】:

请有人帮助我!我快疯了,这应该有效。当我尝试构建我的 Android 项目时,我收到以下错误消息:

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.

错误消息确实提到了 errorText 属性。我以这种方式在 xml 中使用 errorText 属性(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>

这是我在 Kotlin 文件 (full file here) 中定义 errorText 的方式:

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

我只是不明白为什么会这样。是否有某种导入可以放入布局文件中,说明 BindingAdapter 在哪里?我的 Gradle 文件有问题吗?我将它与this question 中的 GitHub 项目进行了比较,该项目显然得到了解决,我看不出与我的项目有什么不同。根据答案,我应该将 Kotlin-kapt 插件添加到我的 Gradle 构建中,我这样做了。我还查看了项目的其余部分并进行了比较。无济于事。您可以找到我的整个 build.gradle file here 以及项目的其余部分。

请帮帮我!

【问题讨论】:

  • 你试过加kapt "com.android.databinding:compiler:$gradleVersion"吗?
  • @AntonHolovin 我现在试过了,但没有帮助。我假设我必须将它添加到应用程序项目的 build.gradle 中?并且版本应该与'com.android.tools.build:gradle:'的版本相同,对吗?那对我不起作用。但也许我做错了什么。我把它推到了 GitHub 你可以在链接下找到它github.com/findusl/OneNightComps/blob/feature/register/Android/…
  • 我花了半个小时试图弄清楚这一点,却发现我错过了右括号。 android:text="@={viewModel.username" 而不是 android:text="@={viewModel.username}"

标签: android kotlin android-databinding androidx


【解决方案1】:

尝试使用

fun bindErrorText(textInputEditText: TextInputEditText, errorText: String) {
 textInputEditText.error = errorText }

【讨论】:

  • 遗憾的是这并不能解决问题。错误消息现在位于不同的行,因为 TextInputEditText 位于不同的行,但其余部分相同。
【解决方案2】:

问题与您将字符串值传递给app:errorText 的方式有关。

使用@{``} 来传递这个值。

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

apply plugin: 'kotlin-kapt' 必须包含在 app/build.gradle 中。

【讨论】:

  • 你这个疯子。非常感谢您认为字符串文字是问题所在。我不明白为什么我不能传递字符串文字,但我不在乎,因为字符串文字无论如何都不好。我只是把它放在那里,因为我认为它更容易测试。当我引用视图模型的变量时,它最终编译并工作。我会将您的答案标记为正确,您可能想为其他人更新它,您的确切解决方案会给出错误data binding error ****msg:Syntax error: no viable alternative at input '&lt;EOF&gt;'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多