How to pass null argument in Android Databinding

Viewed 3577

Why can't pass null value? how can fix it? I can't find any hint from Document.

ERROR

****/ data binding error ****msg:cannot find method onClick(java.lang.Object, java.lang.Object) in class kr.co.app.MyActivity.MyListener file:/Users/jujaeho/projects/app/src/main/res/layout/activity_my.xml loc:24:71 - 24:106 ****\ data binding error ****

CODE

class MyActivity {
  interface MyListener {
    fun onClick(abc: ABC?, count: Int?)
  }
}

<layout>
  <data>
  <variable
    name="handler"
    type="kr.co.app.MyActivity.MyListener" />
  </data>
  <View
    ...
    android:onClick="@{() -> handler.onClick(null, null)}" />
</layout>
2 Answers

I just encountered this problem today, and what I did basically was to cast the null parameters to the types expected in the method parameters. In your case, this should be something like:

<layout>
  <data>
  <import type="ABC" /> // just an illustration, specify the full package
  <variable
    name="handler"
    type="kr.co.app.MyActivity.MyListener" />
  </data>
  <View
    ...
    android:onClick="@{() -> handler.onClick((ABC) null, (int) null)}" />
</layout>

I am not sure about the int casting, but you can try it or use the Integer wrapper class for casting.

if you are intending to pass null why cannot you assign default value for your onClick method

class MyActivity {
  interface MyListener {
    fun onClick(abc: ABC?=null, count: Int?=null)
  }
}

you can pass nothing if you want to pass null

Related