简体   繁体   English

Android 未找到绑定适配器

[英]Android Binding Adapter is not found

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 项目时,出现以下错误消息:

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.错误消息确实提到了 errorText 属性。 I use the errorText attribute in the xml this way ( full xml here ):我以这种方式在 xml 中使用 errorText 属性( 此处为完整的 xml ):

<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 ):这是我在 Kotlin 文件中定义 errorText 的方式( 此处为完整文件):

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?我可以在布局文件中放置某种导入,说明 BindingAdapter 的位置吗? Do I have some wrong with my Gradle files?我的 Gradle 文件有问题吗? I compared it to the GitHub project in this question which apparently got solved and I do not see the difference to my project.我将它与这个问题中的 GitHub 项目进行了比较,该项目显然已解决,但我看不出我的项目有何不同。 According to the answer I should add the Kotlin-kapt plugin to my Gradle build, which I did.根据答案,我应该将 Kotlin-kapt 插件添加到我的 Gradle 构建中,我确实这样做了。 I also looked through the rest of the project and compared.我也翻了一下项目的rest,对比了一下。 To no avail.无济于事。 You can find my whole build.gradle file here as well as the rest of the project.你可以在这里找到我的整个 build.gradle 文件以及项目的 rest。

Please help me!请帮我!

The problem is connected with the way you pass String value to app:errorText . 问题与将String值传递给app:errorText

Use @{``} to pass this value. 使用@ {``}传递此值。

Fixed part of fragment_login.xml: 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. 必须在app/build.gradleapp/build.gradle apply plugin: 'kotlin-kapt'

Try using 尝试使用

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

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.我为此花了一个小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM