I'm trying to eliminate all the warnings of my Android application and one of them is this:
viewModel.value 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.value with safeUnbox() to prevent the warning
Where value is a generic ObservableField
that comes from a super class:
public abstract class BaseDataTypeViewModel<T> extends BaseObservable {
public final ObservableField<T> value = new ObservableField<>();
...
}
And is extented somewhere as a Boolean
:
public class CheckBooleanDataTypeViewModel extends BaseDataTypeViewModel<Boolean> {
...
}
I saw on data binding - safeUnbox warning that the warnings happen because this is a Boolean
and not a boolean
, so I tried to add this: android:checked="@={safeUnbox(viewModel.value)}"
instead of android:checked="@={viewModel.value}"
but then I got an error saying I can't invert the safeUnbox()
method.
****/ data binding error ****msg:The expression android.databinding.DynamicUtil.safeUnbox(viewModelValue) cannot be inverted: There is no inverse for method safeUnbox, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
I understand correctly the 2 separated issues, but do I have to live with the warning to avoid the error or is their a solution to avoid both the warning and the error? What about the @InverseMethod
it is talking about? I didn't manage to add this annotation because the method comes from the android package.