android – can' t cast double to int in xml-ThrowExceptions

Exception or error:

Android studio 3.6

data binding

<SeekBar
            android:id="@+id/seekBar"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:max='@{model.maxDiscountBonuses}'
            android:progress="@{(int) model.discountBonuses}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/currentBonusesTextView" />

But I get error.

How cast double (model.discountBonuses) to int in xml?

How to solve:

You should try it like this

<data>
    <import type="java.lang.Integer"/>
</data>

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:max='@{model.maxDiscountBonuses}'
    android:progress="@{Integer.valueOf(model.discountBonuses)}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/currentBonusesTextView" />

Leave a Reply

Your email address will not be published. Required fields are marked *