Issue
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?
Solution
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.
Answered By - ConstOrVar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.