[英]Android Binding Adapter is not found
请有人帮助我,我快疯了。 这应该有效:当我尝试构建我的 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 属性( 此处为完整的 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>
这是我在 Kotlin 文件中定义 errorText 的方式( 此处为完整文件):
object ViewDataBindingExtensions {
@JvmStatic
@BindingAdapter("errorText")
fun bindErrorText(textInputLayout: TextInputLayout, errorText: String) {
textInputLayout.error = errorText
}
}
我只是不明白为什么会这样。 我可以在布局文件中放置某种导入,说明 BindingAdapter 的位置吗? 我的 Gradle 文件有问题吗? 我将它与这个问题中的 GitHub 项目进行了比较,该项目显然已解决,但我看不出我的项目有何不同。 根据答案,我应该将 Kotlin-kapt 插件添加到我的 Gradle 构建中,我确实这样做了。 我也翻了一下项目的rest,对比了一下。 无济于事。 你可以在这里找到我的整个 build.gradle 文件以及项目的 rest。
请帮我!
问题与将String值传递给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}">
必须在app/build.gradle
中app/build.gradle
apply plugin: 'kotlin-kapt'
。
尝试使用
fun bindErrorText(textInputEditText: TextInputEditText, errorText: String) {
textInputEditText.error = errorText }
另一个不常被讨论的原因是你需要在 "" 标签内进行布局,否则数据绑定不起作用。 我为此花了一个小时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.