Bug Busting: ‘cannot generate view binders’

I’m a big fan of Databinding for Android, and have been using it with much success for a while now.

Every once in a while though, I end up with a baffling error that grinds productivity to a halt.  The most recent error looked like this:

cannot generate view binders java.lang.StackOverflowError

The Problem

I encountered this while trying to bind an android:onLongClick property of a view. I hadn’t done this before, but I bind view model methods to android:onClick all the time.

Without thinking too deeply about it (my mistake) I set up my bound onClongClick()function the same way as I typically do for my onClick() ​functions.

I created a view model function to handle the click:

fun onLongClick() {
  // handle the click

After defining the method in my view model, I added this line to my layout file.

android:onLongClick="@{() -> viewModel.onLongClick()}"

and BOOM, just like that Gradle is yelling at me with a build error and large stack trace with

cannot generate view binders java.lang.StackOverflowError

at the top.

This was quite frustrating as I didn’t see why it should fail when binding android:onClick has always worked before.

The Solution

After a few mintues frustration, I did what I typically do when I’m struggling to understand a Databinding issue and I went back to the documentation.

In the Event Handling 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)

That was my “ah ha!” moment.

A quick lookup of the method signatures of View.OnClickListener and View.OnLongClickListener showed that OnClickListener returns void which is why I’ve never run into this problem before defining viwe model click handlers that return void (or Unit in the case of Kotlin).

I quickly modified my long click handler to look like this:

fun onLongClick() : Boolean {
  // handle the click

  return true

and after rebuilding the project I was back in business. No more build errors and I was back to happy Databinding productivity.

Leave a Reply