4

I'm newbie with Android databinding library.

I have bunch of warnings such as:

warning: viewModel.someBoolean.getValue() is a boxed field but needs to be un-boxed to execute android:checked. This may cause NPE so Data Binding will safely unbox it. You can change the expression and explicitly wrap viewModel.someBoolean.getValue() with safeUnbox() to prevent the warning

It's defined as follows:

In ViewModel

val someBoolean: MutableLiveData<Boolean> = MutableLiveData()

In Layout

<RadioButton
    android:id="@+id/someBooleanRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="@={viewModel.someBoolean}"
    android:text="@string/boolean_description" />

I tried to fix it by adding safeUnbox() :

<RadioButton
    android:id="@+id/someBooleanRadioButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="@={safeUnbox(viewModel.someBoolean)}"
    android:text="@string/boolean_description" />

But I am geting compilation error:

msg:cannot find method safeUnbox(java.lang.Boolean) in class android.databinding.ViewDataBinding

In gradle already defined

dataBinding {
        enabled = true
    }

and kapt 'com.android.databinding:compiler:3.1.4'

Any thoughts how to fix it? Android Studio 3.1.4 Gradle 4.4 Kotlin 1.2.61

P.S. just got duplicate on question. All questions is about how to fix warning, but my question is how to fix compilation error when you adding safeUnbox()

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Max Polkovnik
  • 319
  • 1
  • 5
  • 18
  • @nilesh-rathod I don't think it is duplicate – Max Polkovnik Sep 10 '18 at 07:02
  • https://stackoverflow.com/a/47337166/7666442 – AskNilesh Sep 10 '18 at 07:04
  • @NileshRathod This question is about how to fix "The expression android.databinding.DynamicUtil.safeUnbox(viewModelValue) cannot be inverted", but my question is about how to fix "msg:cannot find method safeUnbox(java.lang.Boolean) in class android.databinding.ViewDataBinding". I think it's a little different, don't you? – Max Polkovnik Sep 10 '18 at 07:12
  • I also consider this as a *not* duplicate – wzieba Oct 07 '18 at 07:43

2 Answers2

4

I am telling about Boolean, this solution is same for Integer, Double, Character etc.

When you have two-way binding, then you can not use safeUnbox() way, because safeUnbox() will not be inverted.

<variable
    name="enabled"
    type="Boolean"/>

....

<Switch
    android:checked="@={enabled}"
    />

Solution 1

Change Boolean to primitive type boolean. So that it never be null, default value of boolean is false.

<variable
    name="enabled"
    type="boolean"/>

Solution 2

A long way is to make method for safeBox and safeUnbox method. See here.

What is safeUnbox() method?

safeUnbox() just check null value and return non-null value. You can see below methods which are defined in Data binding library.

public static int safeUnbox(java.lang.Integer boxed) {
    return boxed == null ? 0 : (int)boxed;
}
public static long safeUnbox(java.lang.Long boxed) {
    return boxed == null ? 0L : (long)boxed;
}
public static short safeUnbox(java.lang.Short boxed) {
    return boxed == null ? 0 : (short)boxed;
}
public static byte safeUnbox(java.lang.Byte boxed) {
    return boxed == null ? 0 : (byte)boxed;
}
public static char safeUnbox(java.lang.Character boxed) {
    return boxed == null ? '\u0000' : (char)boxed;
}
public static double safeUnbox(java.lang.Double boxed) {
    return boxed == null ? 0.0 : (double)boxed;
}
public static float safeUnbox(java.lang.Float boxed) {
    return boxed == null ? 0f : (float)boxed;
}
public static boolean safeUnbox(java.lang.Boolean boxed) {
    return boxed == null ? false : (boolean)boxed;
}
Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

Try deleting the build folder under app directory: app/build.

I encountered this error after removing data-binding from some parts of code in the app. Looks like some bindings generated from previous code were still present in the cache.

Image showing directory location

Devansh Maurya
  • 832
  • 12
  • 11