将 AS gradle 版本升级到 2.3.0 后,数据绑定遇到警告:
警告:selectMap[index] 是一个装箱字段,但需要取消装箱才能执行 selectMap[index] ? @android:color/white : @android:color/透明。这可能会导致 NPE,因此数据绑定会安全地将其拆箱。您可以更改表达式并使用 safeUnbox() 显式包装 selectMap[index] 以防止警告
selectMap 是一个 ObservableMap,然后我搜索了这个警告,但只有很少的讨论并且没有修复它
Android Studio 2.3.0-alpha1:Databinding + int 拆箱导致编译错误
我按照链接中的方式,将 selectMap[index]
修改为 safeUnbox(selectMap[index])
但出现语法错误。
那么有人知道如何解决这个警告吗?
编辑:这是 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>
构建成功,但出现警告(我在上面)。
原文由 Ivan 发布,翻译遵循 CC BY-SA 4.0 许可协议
我有同样的警告,在我的例子中,将变量声明从 布尔 类型更改为 布尔 类型解决了这个问题:
从:
到:
所以,也许你可以尝试: