Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 关于数据绑定、实时数据和可绘制数据的问题_Android_Android Databinding_Android Livedata - Fatal编程技术网

Android 关于数据绑定、实时数据和可绘制数据的问题

Android 关于数据绑定、实时数据和可绘制数据的问题,android,android-databinding,android-livedata,Android,Android Databinding,Android Livedata,我试图用实时数据绑定一个绘图表,但遇到了这个错误 java.lang.RuntimeException: Failed to call observer method Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0 我的xml片段(仅粘贴了相关部分) 任何帮助都将不胜感激。谢谢 iconObservable.value 在您的xml上尝试此操作,它将起作用您可以在xml中使用app:ima

我试图用实时数据绑定一个绘图表,但遇到了这个错误

java.lang.RuntimeException: Failed to call observer method
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
我的xml片段(仅粘贴了相关部分)

任何帮助都将不胜感激。谢谢

iconObservable.value
在您的xml上尝试此操作,它将起作用

您可以在xml中使用
app:imageResource
属性:


...
在viewModel中,应具有以下属性:

val iconObservable:MutableLiveData
您可以使用以下方法:

private fun determinateweathericon(天气描述:字符串){
iconObservable.value=when(天气描述){
“雷雨”->R.drawable.Reuterstorm\u图标
“细雨”->R.drawable.rain\u图标
“雨”->R.drawable.Rain\u图标
“雪”->R.drawable.Snow\u图标
“清除”->R.drawable.sun\u图标
“云”->R.drawable.cloudy\u图标
else->R.drawable.cloudy\u图标
}
}
您不需要
postValue
,除非您正在后台线程中设置该值

在片段类中,应该将
viewModel
lifecycleOwner
设置为
绑定

覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看?{
绑定=碎片绑定。充气(充气器、容器、假)
binding.viewModel=viewModel
binding.lifecycleOwner=viewLifecycleOwner
返回binding.root
}

您是否将
viewModel
设置为fragment/activity中的binding类?@Manoharred override fun onCreateView(充气机:布局充气机,容器:视图组?,savedInstanceState:捆绑?):视图?{binding=DataBindingUtil.inflate(layoutInflater,R.layout.fragment_daily_weather,container,false)binding.lifecycleOwner=this return binding.root}这样设置仍然会给我一个错误。android:src=“@{ContextCompat.getDrawable(context,viewModel.iconObservable.intValue())}”谢谢!使用app:imageResource为我修复了它。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <import type="androidx.core.content.ContextCompat" />
        <variable name="location" type="String"/>
        <variable name="viewModel"
            type="com.marty.dang.polarpointsweatherapp.presentation.viewmodel.DailyWeatherViewModel"/>
    </data>
        <ImageView
            android:id="@+id/homeFrag_weather_icon"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="40dp"

            android:src="@{ContextCompat.getDrawable(context, viewModel.iconObservable)}"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/homeFrag_title_text_view" />
val iconObservable: MutableLiveData<Int> by lazy { MutableLiveData<Int>() }

    private fun determineWeatherIcon(weatherDescription: String)  {
        val icon = when(weatherDescription){
            "Thunderstorm" -> R.drawable.thunderstorm_icon
            "Drizzle" -> R.drawable.rain_icon
            "Rain" -> R.drawable.rain_icon
            "Snow" -> R.drawable.snow_icon
            "Clear" -> R.drawable.sun_icon
            "Clouds" -> R.drawable.cloudy_icon
            else -> R.drawable.cloudy_icon
        }

        iconObservable.postValue(icon)
    }
var weatherIcon = R.drawable.snow_icon

private fun determineWeatherIcon(weatherDescription: String)  {
    weatherIcon = when(weatherDescription){
        "Thunderstorm" -> R.drawable.thunderstorm_icon
        "Drizzle" -> R.drawable.rain_icon
        "Rain" -> R.drawable.rain_icon
        "Snow" -> R.drawable.snow_icon
        "Clear" -> R.drawable.sun_icon
        "Clouds" -> R.drawable.cloudy_icon
        else -> R.drawable.cloudy_icon
    }
}

android:src="@{ContextCompat.getDrawable(context, viewModel.weatherIcon)}"