数据绑定 - safeUnbox 警告 [英] data binding - safeUnbox warning
问题描述
在我将 AS gradle 版本升级到 2.3.0 后,数据绑定遇到警告:
after i upgrade AS gradle version to 2.3.0, data binding encounter a warning :
警告:selectMap[index] 是一个装箱字段,但需要取消装箱才能执行 selectMap[index] ?@android:color/white : @android:color/transparent.这可能会导致 NPE,因此数据绑定会安全地将其拆箱.您可以更改表达式并使用 safeUnbox() 显式包装 selectMap[index] 以防止警告
Warning:selectMap[index] is a boxed field but needs to be un-boxed to execute selectMap[index] ? @android:color/white : @android:color/transparent. This may cause NPE so Data Binding will safely unbox it. You can change the expression and explicitly wrap selectMap[index] with safeUnbox() to prevent the warning
selectMap 是一个 ObservableMap,然后我搜索了这个警告,但只有很少的讨论并且没有修复它
selectMap is an ObservableMap, then i search this warning but got just few discussions and did not fix it
Android Studio 2.3.0-alpha1:数据绑定 + int 拆箱导致编译错误
我按照链接中的方法,将 selectMap[index]
修改为 safeUnbox(selectMap[index])
但出现语法错误.
I follow the way in the links, modify selectMap[index]
to safeUnbox(selectMap[index])
but got syntax error.
那么有人知道如何解决这个警告吗?
So anyone know how to fix this warning?
这是xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<data class="SupportCountryViewHolderBinding">
<variable
name="viewModel"
type="com.goodarc.care_about.activity.account.support_country.SupportCountryHolderViewModel" />
<variable
name="dataSource"
type="com.goodarc.care_about.module.assets_file.SupportCountry" />
<variable
name="selectMap"
type="android.databinding.ObservableMap<Integer, Boolean>" />
<variable
name="index"
type="int" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@{selectMap[index] ? @android:color/white : @android:color/transparent}"
android:onClick="@{(v) -> viewModel.onItemSelectListener(selectMap, index)}"
android:orientation="vertical"
android:padding="20dp">
<TextView
style="@style/TitleLabel2"
android:layout_gravity="center_vertical|start"
android:text="@{dataSource.display}"
android:textColor="@{selectMap[index] ? @android:color/black : @android:color/white}"
tools:text="Taiwan (+886)" />
</LinearLayout>
构建成功,但出现警告(我在上面).
Build is succeed, but warning come out(i past above).
推荐答案
我有同样的警告,就我而言,将变量声明从 Boolean 类型更改为 boolean 类型解决问题:
I had the same warning, in my case changing the variable declaration from Boolean type to boolean type solve the problem:
来自:
<variable
name="readOnly"
type="Boolean" />
致:
<variable
name="readOnly"
type="boolean" />
所以,也许你可以试试:
So, maybe you can try with:
<variable
name="selectMap"
type="android.databinding.ObservableMap<Integer, boolean>" />
这篇关于数据绑定 - safeUnbox 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!