首页 > 解决方案 > 未应用库项目中的 DataBinding BindingAdapter

问题描述

我正在创建一个多模块项目。我想在模块中共享 BindingAdapter。但是,会出现以下错误。

com.github.takahirom.bindingadapter_in_library.databinding
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:customText' with parameter type java.lang.String on android.widget.TextView. file:/Users/takahirom/git/bindingadapterinlibrary/app/src/main/res/layout/content_main.xml loc:16:34 - 16:47 ****\ data binding error ****

    at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)
    at android.databinding.annotationprocessor.ProcessDataBinding.doProcess(ProcessDataBinding.java:109)
    at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:73)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:99)

这是示例项目。 https://github.com/takahirom/databinding-in-library-sample

应用程序模块https://github.com/takahirom/databinding-in-library-sample/blob/master/app/src/main/res/layout/content_main.xml

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:customText="@{`Hello World!`}"

库模块https://github.com/takahirom/databinding-in-library-sample/blob/master/library/src/main/java/TestBinding.kt(如果我把这个文件放在app模块中,错误不会发生)

@BindingAdapter("app:customText")
fun customText(textView: TextView, text: String) {
    textView.text = "** $text **"
}

标签: androidandroid-databindingandroid-binding-adapter

解决方案


改用这个

@BindingAdapter("customText")

绑定适配器中不考虑名称空间


推荐阅读