Kotlin和Databinding Int值null檢查問題

[英]Kotlin & Databinding Int value null check issue


This is the weirdest problem I faced ever.

So again kotlin & data binding made me stuck. I start my question with the code.

所以kotlin和數據綁定再次讓我陷入困境。我用代碼開始提問。

Model class

data class ModelBottomItem(
        @StringRes val name: Int,
        @DrawableRes val image: Int,
        val gender: Int = 0,
        val enabled: Boolean = true,
        val disabledIconColor: Int = 0)
    : BaseModel()

Layout code

<variable
    name="item"
    type="myModel"/>


<TextView
    android:text="@{item.gender == 0 ? @string/male : @string/female}"
    ...
    />

Debug trace list

調試跟蹤列表

0 = {ModelBottomItem@6406} "ModelBottomItem(name=2131755043, image=2131230862, gender=0, enabled=true, disabledIconColor=0)"
08-10 12:57:12.388 24902-24902/acr.browser.barebones E/AndroidRuntime: FATAL EXCEPTION: main
    Process: acr.browser.barebones, PID: 24902
    java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference

blah, blah !!

等等,等等!!

If i change item.gender == null

Then the code is not able to build. It fails with below error.

error: incomparable types: int and <null>
                itemGenderJavaLangObjectNull = (itemGender) == (null);

Understand problem in two lines.

  • When I check item.gender == null shows error incomparable types: int and <null>.
  • 當我檢查item.gender == null顯示錯誤無法比較的類型:int和

  • And when I check for item.gender == 0, then code will crash saying int java.lang.Integer.intValue()' on a null object reference in case of 0 at run time. Even you can see debug trace, my model item is 0, it is not **null**. Even I have setted default value to gender, that is val gender: Int = 0. Then how can it be null or 0?
  • 當我檢查item.gender == 0時,如果在運行時為0,代碼將在空對象引用上對int java.lang.Integer.intValue()進行崩潰。即使你可以看到調試跟蹤,我的模型項是0,它不是** null **。即使我已將默認值設置為性別,即val性別:Int = 0.那么它如何為null或0?

2 个解决方案

#1


1  

For this case use toIntOrNull() like this

對於這種情況,請使用toIntOrNull()

 item.gender.toString().toIntOrNull()

Example

    val numb=""
    val numb2="7"
    val print1=numb.toIntOrNull()
    println(print1) //out put is null
    val print2=numb2.toIntOrNull()
    println(print2) // output is 7

your data model use nullable Int?

你的數據模型使用可空的Int?

 gender: Int? = 0 as parameter

like this

 data class ModelBottomItem(
    @StringRes val name: Int,
    @DrawableRes val image: Int,
    val gender: Int?= 0,
    val enabled: Boolean = true,
    val disabledIconColor: Int = 0)
: BaseModel()

#2


0  

Looks like you havent properly called yourActivityBinding.setItem(your_item) in your Activity.

看起來您沒有在Activity中正確調用yourActivityBinding.setItem(your_item)。

Try adding a break point in your activity to confirm you are not binding a null item to the layout

嘗試在活動中添加斷點以確認您沒有將null項綁定到布局


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/08/10/72602095d1c2671f1b49c76892855dc3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com