簡體   English   中英

為什么我會收到 Kotlin.Unit 錯誤以及如何解決?

[英]Why do i get the Kotlin.Unit error and how to fix it?

I have a function refreshPictureOfDay() in my view model which calls a retrofit service query to retrive the PictureOfDay object

private val _pictureOfDay = MutableLiveData<PictureOfDay>()
    val pictureOfDay: LiveData<PictureOfDay>
        get() = _pictureOfDay

 init {
        viewModelScope.launch {
            refreshPictureOfDay()
        }
    }

 private suspend fun refreshPictureOfDay() {
        withContext(Dispatchers.IO) {
            try {
                _pictureOfDay.postValue(
                    NasaApi.retrofitService.getPictureOfTheDay()
                )
            } catch (err: Exception) {
                Log.e("refreshPictureOfDay", err.printStackTrace().toString())
            }
        }
    }

然后我有一個綁定適配器

@BindingAdapter("imageUrl")
fun bindImage(imageView: ImageView, url: String?) {
    Picasso.with(imageView.context)
        .load(url)
        .placeholder(R.drawable.placeholder_picture_of_day)
        .error(R.drawable.placeholder_picture_of_day)
        .into(imageView)
}

使用這個綁定適配器,我嘗試在我的 XMl 文件中設置圖像

 app:imageUrl="@{viewModel.pictureOfDay.url}"

我不知道為什么,但它不工作,並記錄以下錯誤在此處輸入圖像描述

這是今天的圖片 class

data class PictureOfDay(@Json(name = "media_type") val mediaType: String, val title: String,
                        val url: String)

我仍然是初學者,所以任何幫助都會得到幫助

printStackTrace()將堆棧跟蹤寫入控制台(這與 Android 日志,又名 LogCat 不同)。 printStackTrace() function 不返回任何內容。 When a function doesn't return anything, Kotlin treats it as if it returns kotlin.Unit , which is why your log only says kotlin.Unit .

如果要記錄堆棧跟蹤,請執行以下操作:

Log.e("refreshPictureOfDay", Log.getStackTraceString(err))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM