TransWikia.com

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

Stack Overflow Asked on December 1, 2020

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

2 Answers

Add a null-check when you assign the value

playState != null ? .. : ..

But also reconsider why you use a livedata there..
You could also just bind the state and set it within an observer.

Correct answer by Tobi on December 1, 2020

Whether you are using MVP or MVVM architecture for your implementation. It is not good idea to have business logic manipulation on UI part of code in my opinion. The PlayState related manipulation need to be done on ViewModel or Presenter. So I suggest add this LiveData as attribute of the ViewModel/Presenter associated with your fragment, initialize it in init block of ViewModel/Presenter class. Inside your fragment, assuming name of xml for your fragment is example_layout.xml add following code inside OnCreateView function of fragment as follows :

LayoutExampleBinding.inflate(inflater, container, false).also {
            it.lifecycleOwner = viewLifecycleOwner
            it.viewModel = <ViewModel>
            it.playState = <ViewModel>.playState
}.root

(Or bind Presenter instead of ViewModel if using MVP architecture)

Answered by Anand Kamathi on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP