Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find the setter for attribute with parameter

I am working on DataBinding with BindingAdapter. Here is my custom method.

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

And in xml file i am calling it like

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

But it is showing error

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 ****

I have checked this and this thread but somehow it is not helping me, as you can see i am passing int from xml and in BindingAdapter also i have mentioned LinearLayout with int value.

Even i have another method, where just parameters are different and its working fine

@BindingAdapter({"bind:image_round"}) 
public static void loadRoundImage(ImageView imageView, String url)
like image 845
Ravi Avatar asked Oct 22 '16 06:10

Ravi


4 Answers

Make sure in app level gradle, you have apply plugin: 'kotlin-kapt'

like image 64
Kishan Solanki Avatar answered Nov 15 '22 11:11

Kishan Solanki


Your @BindingAdapter definition looks a little bit odd to me

@BindingAdapter("{bind:fadevisible}")

This is not the same like

@BindingAdapter({"bind:fadevisible"})

or

@BindingAdapter("bind:fadevisible")

which should work perfectly fine.

like image 27
tynn Avatar answered Nov 15 '22 11:11

tynn


I had this problem with binding to ImageView and unlike your case, the definition of my binding adapter was correct but still, the IDE kept giving me this error message. After spending many hours on searching for the cause, I figured that the namespace that I use in xml layout file needs to be exactly what I declared in @BindingAdapter.

So, if my xml is like below:

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

Then my binding method should be as below:

@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);
    }
}

Note that binding method annotation indicates the namespace in it , i.e. @BindingAdapter({"app:image_url"}) exactly as it is used in layout file app:image_url="@{item.logoUrl}"

So unlike what is said in most tutorials, don't use @BindingAdapter({"bind:image_url"}) in your binding method and app:image_url="@{item.logoUrl}" in your xml file.

like image 12
Akram Avatar answered Nov 15 '22 11:11

Akram


You try

 @BindingAdapter("bind:fadevisible")
like image 4
Emre Aslan Avatar answered Nov 15 '22 09:11

Emre Aslan