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

Viewed 39

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())
            }
        }
    }

Then i have a binding adapter

@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)
}

Using this binding adapter i try to set the image in my XMl file

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

I don't know why but it is not working and it logs the following error enter image description here

This is the PictureOfDay class

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

I am still a beginner so any help would be apprciated

1 Answers

printStackTrace() writes the stack trace to the console (which is not the same thing as the Android Log, aka LogCat). The printStackTrace() function doesn't return anything. 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.

If you want to log the stack trace, do this:

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