Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass null argument in Android Databinding

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>
like image 573
illusionJJ Avatar asked Nov 13 '18 11:11

illusionJJ


1 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.

like image 139
idrisadetunmbi Avatar answered Nov 07 '22 05:11

idrisadetunmbi