safeUnbox()不能反转 [英] safeUnbox() cannot be inverted
问题描述
我正试图消除我的Android应用程序的所有警告,其中之一是:
I'm trying to eliminate all the warnings of my Android application and one of them is this:
viewModel.value是一个装箱的字段,但需要取消装箱才能执行android:checked.这可能会导致NPE,因此数据绑定"将安全地将其拆箱.您可以更改表达式,并使用safeUnbox()显式包装viewModel.value以防止出现警告
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
其中值是来自超类的通用ObservableField
:
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<>();
...
}
并扩展为Boolean
:
public class CheckBooleanDataTypeViewModel extends BaseDataTypeViewModel<Boolean> {
...
}
我在数据绑定-safeUnbox警告上看到发生警告,因为这是Boolean
而不是boolean
,因此我尝试添加以下内容:android:checked="@={safeUnbox(viewModel.value)}"
而不是android:checked="@={viewModel.value}"
,但是随后出现错误消息,提示我无法反转safeUnbox()
方法.
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.
****/数据绑定错误**** msg:不能将表达式android.databinding.DynamicUtil.safeUnbox(viewModelValue)反转:方法safeUnbox没有反向,您必须在该接口上添加@InverseMethod批注表示在双向绑定表达式中使用哪种方法的方法
****/ 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
我正确理解了两个分开的问题,但是我必须忍受警告以避免错误吗?或者它们是避免警告和错误的解决方案吗?它在说什么@InverseMethod
呢?我没有设法添加此批注,因为该方法来自android包.
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.
推荐答案
我没有以这种特定方式使用Android体系结构组件或数据绑定库,但我认为我仍然可以提供帮助.
I haven't worked with Android Architecture Components or with the Data Binding libraries in this particular way, but I think I can still help.
在您的XML中,您已经做到了:
Within your XML, you've got this:
android:checked="@={viewModel.value}"
系统正在向您发出警告,因为它希望您知道在viewModel.value
为null
的情况下,它会做一些特殊的事情(大概是false
的行为).它是通过safeUnbox()
方法完成的.
The system is giving you a warning because it wants you to know that in the case where viewModel.value
is null
, it's going to do something special (behave as though it were false
instead, presumably). It does this via the safeUnbox()
method.
要解决该警告,建议将safeUnbox()
调用设为显式.您之所以不能这样做,是因为没有safeUnbox()
的逆"可以将 从boolean
返回到Boolean
.
To solve the warning, it's suggesting making the safeUnbox()
call explicit. You can't do that because there's no "inverse" of safeUnbox()
to go back from boolean
to Boolean
.
但这听起来不像您必须使用safeUnbox()
;您可以创建自己的将Boolean
转换为boolean
的方法,然后可以使用建议的注释来声明哪种方法将从boolean
转换为Boolean
.
But it doesn't sound like you have to use safeUnbox()
; you could create your own method that converts Boolean
to boolean
, and then you could use the suggested annotation to declare which method will convert back from boolean
to Boolean
.
public class MyConversions {
@InverseMethod("myBox")
public static boolean myUnbox(Boolean b) {
return (b != null) && b.booleanValue();
}
public static Boolean myBox(boolean b) {
return b ? Boolean.TRUE : Boolean.FALSE;
}
}
现在您可以将XML更改为:
Now you can change your XML to:
android:checked="@={com.example.stackoverflow.MyConversions.myUnbox(viewModel.value)}"
我希望这会有所帮助.如果事实证明我离基地很远,请告诉我;我想了解更多有关此主题的信息.
I hope this helps. If it turns out that I'm way off-base, let me know; I'd love to learn more about this topic.
我从 https了解到的大部分内容://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873
这篇关于safeUnbox()不能反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!