Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

android - data binding - safeUnbox warning

after i upgrade AS gradle version to 2.3.0, data binding encounter a warning :

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 is an ObservableMap, then i search this warning but got just few discussions and did not fix it

Android Studio 2.3.0-alpha1: Databinding + int unboxing causes compile errors

Databinding - data object is null on API 15-18

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?


Edit : Here is the xml file code

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

Build is succeed, but warning come out(i past above).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had the same warning, in my case changing the variable declaration from Boolean type to boolean type solve the problem:

From:

<variable
        name="readOnly"
        type="Boolean" />

To:

<variable
        name="readOnly"
        type="boolean" />

So, maybe you can try with:

<variable
    name="selectMap"
    type="android.databinding.ObservableMap&lt;Integer, boolean&gt;" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...