使用LiveData在ImageView上使用DataBinding时出现IllegalArgumentException

问题描述 投票:0回答:2

我试图使用DataBinding将图像添加到ImageView。

我从Room数据库中获取一个对象,并将标题和图像URL公开为LiveData。我可以设置标题,但设置图像失败。

这是错误日志

 Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter imageUrl
    at com.sample.databinding.detail.ArticleDetailsViewModel$Companion.loadImage(Unknown Source:7)
    at com.sample.databinding.detail.ArticleDetailsViewModel.loadImage(Unknown Source:2)
    at com.sample.databinding.detail.databinding.ActivityArticleDetailsBindingImpl.executeBindings(ActivityArticleDetailsBindingImpl.java:198)
    at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:448)
    at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:420)
    at androidx.databinding.ViewDataBinding$OnStartListener.onStart(ViewDataBinding.java:1633)
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:215)

这是生成的文件中失败的行

if ((dirtyFlags & 0x19L) != 0) {
        // api target 1

        com.sample.databinding.detail.ArticleDetailsViewModel.loadImage(this.backdrop, articleDetailsViewModelImageUrlGetValue);
    }

这是我的代码

activity_details.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="articleDetailsViewModel"
            type="com.monzo.androidtest.detail.ArticleDetailsViewModel" />
    </data>

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:imageSource="@{articleDetailsViewModel.imageUrl}"/>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@{articleDetailsViewModel.articleTitle}"/>


        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

ActivityDetailsViewModel.kt

package com.sample.databinding.detail

import android.app.Application
import android.widget.ImageView
import androidx.core.text.HtmlCompat
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.bumptech.glide.Glide
import com.sample.databinding.articles.model.Article
import com.sample.databinding.data.ArticlesDao
import kotlinx.coroutines.*


class ArticleDetailsViewModel(
        val database: ArticlesDao,
        val application: Application,
        val articleID: String) : ViewModel() {

    val article = database.getSpecificPost(articleID)

    val imageUrl = Transformations.map(article) {article.value?.thumbnail}
    val  articleTitle = Transformations.map(article) {article.value?.title}




    companion object {
        @JvmStatic
        @BindingAdapter("bind:imageSource")
        fun loadImage(view: ImageView, imageUrl: String) {
            if (!imageUrl.isEmpty()) {
                Glide.with(view.getContext())
                        .load(imageUrl)
                        .into(view)

            }

        }
    }
}
android kotlin android-databinding android-architecture-components android-livedata
2个回答
1
投票

它应该是

imageUrl?.let {
     Glide.with(view.getContext())
          .load(imageUrl)
          .into(view)
}

1
投票

看到你编写了一个很好的代码,但是绑定适配器存在问题

   @JvmStatic
        @BindingAdapter("bind:imageSource")
        fun loadImage(view: ImageView, imageUrl: String) {
            if (!imageUrl.isEmpty()) {
                Glide.with(view.getContext())
                        .load(imageUrl)
                        .into(view)
            }
        }

在此代码中,您期望一个非null imageUrl,但在运行时,此值为null。所以这是一个例外。

要解决这个问题,你必须提到参数imageUrl nullable。然后你必须检查imageUrl是null还是空。所以你的绑定适配器方法将是这样的:

 @JvmStatic
        @BindingAdapter("bind:imageSource")
        fun loadImage(view: ImageView, imageUrl: String?) {
            if (!imageUrl.isNullOrBlank()) {
                Glide.with(view.getContext())
                        .load(imageUrl)
                        .into(view)
            }
        }

希望这可以帮助。快乐编码:)

© www.soinside.com 2019 - 2024. All rights reserved.