Monday, 16 November 2020

How can I set default value for data binding in Android Studio?

I have assigned the data for variable playState before fire Code A , but the Code A still get the following error information.

method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter aEPlayState

I think playState is a LiveData, the initial value is null, it maybe cause error before I assign the data to it.

How can I fix it? is Code B right?

Code A

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="android.view.View" />
        <import type="androidx.lifecycle.LiveData" />
        <import type="info.dodata.voicerecorder.model.EPlayState" />
      
        <variable
            name="playState"
            type="LiveData&lt;EPlayState>" />
      
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"      
        android:orientation="vertical">

            <ImageButton
                android:id="@+id/btnPlay"
                android:layout_width="@dimen/round_button_small"
                android:layout_height="@dimen/round_button_small"  
                app:srcCompat="@drawable/play_play"
                app:iamgeForPlayPause="@{playState}"
                
            />
     </LinearLayout>

</layout>


@BindingAdapter("app:iamgeForPlayPause")
fun imageForPlayPause(aImageButton: ImageButton, aEPlayState: EPlayState) {
    ...
}

enum class EPlayState {
    STOPPED,
    PLAYING,
    PAUSED
}

Code B

<variable
   name="playState"
   type="LiveData&lt;EPlayState>" 
   default="EPlayState.STOPPED"
/>


from How can I set default value for data binding in Android Studio?

No comments:

Post a Comment