在执行代码A之前,我已经为变量playState
分配了数据,但是代码A仍然遇到以下错误信息。
kotlin.jvm.internal.Intrinsics.checkNotNullParameter方法,参数aEPlayState
我认为playState
是一个LiveData
,其初始值为null,在我将数据分配给它之前可能会导致错误。
我该如何解决?代码B正确吗?
代码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<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
}
代码 B
<variable
name="playState"
type="LiveData<EPlayState>"
default="EPlayState.STOPPED"
/>
app:iamgeForPlayPause="@{playState!=null?playState:EPlayState.STOPPED}"
。 - ADM