找不到带有参数的属性的 setter

Cannot find the setter for attribute with parameter

我正在 DataBindingBindingAdapter 合作。这是我的自定义方法。

@BindingAdapter("{bind:fadevisible}")
public static void setFadeVisible(LinearLayout view, int visible) {
    Log.e("Bindings", "setFadeVisible: ");
}

在 xml 文件中,我将其命名为

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:fadevisible="@{1}"/>

但是显示错误

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:fadevisible' with parameter type int on android.widget.LinearLayout. file:\app\src\main\res-main\layout\activity_detail.xml loc:236:31 - 236:54 ****\ data binding error ****

我已经检查了 and this 线程,但不知何故它对我没有帮助,正如你所看到的,我正在从 xml 和 BindingAdapter 传递 int 我也有提到 LinearLayout,价值 int

甚至我还有另一种方法,其中只是参数不同并且工作正常

@BindingAdapter({"bind:image_round"}) 
public static void loadRoundImage(ImageView imageView, String url)

你试试

 @BindingAdapter("bind:fadevisible")

我觉得你的 @BindingAdapter 定义有点奇怪

@BindingAdapter("{bind:fadevisible}")

这跟

不一样
@BindingAdapter({"bind:fadevisible"})

@BindingAdapter("bind:fadevisible")

应该可以正常工作。

我在绑定到 ImageView 时遇到了这个问题,与您的情况不同,我的绑定适配器的定义是正确的,但是 IDE 一直给我这个错误消息。在花了很多时间寻找原因后,我发现我在 xml 布局文件中使用的命名空间需要与我在 @BindingAdapter.

中声明的完全相同

所以,如果我的 xml 如下所示:

<ImageView
    android:id="@+id/logo"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    app:image_url="@{item.logoUrl}"
/>

那么我的绑定方式应该是这样的:

@BindingAdapter({"app:image_url"})
public static void loadImage(ImageView view, String logoUrl) {
    if (logoUrl == null) {
        view.setImageResource(R.drawable.ic_place_holder);
    } else {
        Glide.with(getContext()).load(logoUrl).crossFade().into(view);
    }
}

请注意,绑定方法注释指示其中的命名空间,即 @BindingAdapter({"app:image_url"}) 与布局文件中使用的完全相同 app:image_url="@{item.logoUrl}"

因此,与大多数教程中所说的不同,不要在绑定方法中使用 @BindingAdapter({"bind:image_url"}) 和在 xml 中使用 app:image_url="@{item.logoUrl}"文件。

我最初将 customBindidingAdapter 定义为私有:

@BindingAdapter("setPriorityColor")
private static void getPriorityColor(TextView textView, int priority) {
}

确保在应用级别 gradle,您有 apply plugin: 'kotlin-kapt'

如果您正在处理多个模块,那么请添加答案

 @BindingAdapter("fadevisible")

该模块在模块中应该有如下代码-> build.gradle.

dataBinding {
    enabled = true
}

享受快乐编码。 :)

在我的特殊情况下,我的 BindingAdapter 两个 参数和 requireAll,而我忽略了将其中一个放在元素上我的布局 XML。所以,像这样:(Kotlin,我知道)

@BindingAdapter("app:arg1", "app:arg2", requireAll = true)
fun MyAdapter(view: ImageView, x: String, y: Int) {
    // ...
}
<Element app:arg1="@{"foo"}"/>

错误大致是 Cannot find the setter for attribute "app:arg1" with parameter String 这是完全正确的,没有这样的适配器; two 个参数只有一个。

发生这种情况的一个暗示是 Android Studio 通过将 MyAdapter 着色为灰色来指示它是一个未使用的函数。

显然,如 "there is no adapter for app:arg1 of type String but there is one for..."(当 一个 的属性名称匹配时)的更多 eloquent 错误消息将不胜感激,但我不会坚持我的呼吸。

除了@BindingAdapter 的改进 (我的在一个版本中工作正常,而在另一个版本中却不行), 将 Build gradle 版本升级到最新版本对我有用。