Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 628 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with higher order function as a binding adapter

#1
I am running into issues trying to take a function as a parameter in a binding adapter using Kotlin/Android databinding. This example code throws `e: error: cannot generate view binders java.lang.StackOverflowError` when building with no other useful info in the log.

Here is my binding adapter:

@JvmStatic
@BindingAdapter("onDelayedClick")
fun onDelayedClick(view: View, function: () -> Unit) {
// TODO: Do something
}

XML:

<View
android:id="@+id/test_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:onDelayedClick="@{() -> viewModel.testFunction()}"/>

and method in my ViewModel:

fun testFunction() = Unit

I have been struggling with this for a bit now and nothing I've tried works, so any help is appreciated.

Reply

#2
The declaration `() -> Unit` suggests a function which takes no input and returns nothing (`Unit` is the return type in this statement). Your function should look like this:

fun testFunction() = {}
Reply

#3
In the [Event Handling](

[To see links please register here]

) section I came across this line:

In Listener Bindings, only your return value must match the expected return value of the listener (unless it is expecting void)

For more about error :

> cannot generate view binders java.lang.StackOverflowError

read [this](

[To see links please register here]

) article. hope it will help you!!
Reply

#4
Use `function: Runnable` instead of `function: () -> Unit`.

Android's data-binding compiler generates java code, to which, your kotlin function's signature looks like `void testFunction()`, as kotlin adapts Unit as void when calling from java.

On the other hand, `() -> Unit` looks like `kotlin.jvm.functions.Function0` which is a function which takes 0 inputs and returns Unit.INSTANCE.

As you can see these two function signatures don't match, and that's why the compilation fails.
Reply

#5
Put `apply plugin: 'kotlin-kapt'` in build.gradle

Then you can create Binding Adapter like

@JvmStatic
@BindingAdapter("onDelayedClick")
fun onDelayedClick(view: View, function: () -> Unit) {
// TODO: Do something
}
And XML Like

<View
android:id="@+id/test_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:onDelayedClick="@{viewModel.testFunction}"/>

And VM Like

val testFunction = {
// TODO: Do something
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through