4

I wrote a custom BindingAdapter for when the close icon on a Chip is being clicked:

@BindingAdapter("onCloseClicked")
fun Chip.onCloseClicked(onCloseClicked: () -> Unit) {
    setOnCloseIconClickListener { onCloseClicked() }
}

I bind it in my layout like this:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="viewModel"
            type="com.example.playground.MyViewModel" />

    </data>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="chip 1"
                app:closeIconEnabled="true"
                app:onCloseClicked="@{() -> viewModel.chip1CloseClicked()}" />

            <com.google.android.material.chip.Chip
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="chip 2"
                app:closeIconEnabled="true" />

    </LinearLayout>
</layout>

When I only bind 1 chip (like above), all works fine. When I also bind the 2nd chip like:

        <com.google.android.material.chip.Chip
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chip 2"
            app:closeIconEnabled="true"
            app:onCloseClicked="@{() -> viewModel.chip2CloseClicked()}" />

the app doesn't build anymore due to a databinding error:

com/example/playground/databinding/ActivityMainBindingImpl.java
error: missing return statement

Any ideas on why this occurs and how to fix it?

Boken
  • 4,825
  • 10
  • 32
  • 42
fweigl
  • 21,278
  • 20
  • 114
  • 205

2 Answers2

4

That problem has been already discussed in that question - data binding compiler generates invalid java code:

// Listener Stub Implementations
// callback impls
public final kotlin.Unit _internalCallbackInvoke(int sourceId ) {
    switch(sourceId) {
        case 1: {
            // localize variables for thread safety
            // viewModel
            com.example.playground.MyViewModel viewModel = mViewModel;
            // viewModel != null
            boolean viewModelJavaLangObjectNull = false;



            viewModelJavaLangObjectNull = (viewModel) != (null);
            if (viewModelJavaLangObjectNull) {


                viewModel.chip1CloseClicked();
            }
            return null;
        }
        case 2: {
            // localize variables for thread safety
            // viewModel
            com.example.playground.MyViewModel viewModel = mViewModel;
            // viewModel != null
            boolean viewModelJavaLangObjectNull = false;



            viewModelJavaLangObjectNull = (viewModel) != (null);
            if (viewModelJavaLangObjectNull) {


                viewModel.chip2CloseClicked();
            }
            return null;
        }
    }
}

Here default branch with return statement is missing.

To solve it, in your case you can change signature of function, marked with @BindingAdapter annotation, to next one

@BindingAdapter("onCloseClicked")
fun Chip.onCloseClicked(clickListener: View.OnClickListener?) {
    setOnCloseIconClickListener(clickListener)
}

After that changes applied, project will be successfully compiled.

ConstOrVar
  • 2,025
  • 1
  • 11
  • 14
1

I had the same problem and I resolved it by changing the return type of chip1CloseClicked function to Void? and my binding adapter like this:

@BindingAdapter("onCloseClicked")
fun Chip.onCloseClicked(onCloseClicked: () -> Void) {
    setOnCloseIconClickListener { onCloseClicked() }
}

In this case the compiler can generate the right Java code from your Kotlin code.