数据绑定-safeUnbox警告 [英] data binding - safeUnbox warning

查看:90
本文介绍了数据绑定-safeUnbox警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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拆箱会导致编译错误

数据绑定-API 15-18中的数据对象为空

我按照链接中的方法进行操作,将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&lt;Integer, Boolean&gt;" />

    <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>

构建成功,但出现警告(上面的i).

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&lt;Integer, boolean&gt;" />

这篇关于数据绑定-safeUnbox警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆