Using a nullable string resource id from view model in Android data binding

I am trying to map API errors (exceptions) to String resources in my view model. My view model looks like the following.

class AccountViewModel @Inject constructor(accountRepository: AccountRepository) : ViewModel() {

  val isLoadingProfile = MutableLiveData(false)
  val profile = MutableLiveData<Profile>()
  val profileLoadError = MutableLiveData<Int>()

  fun loadProfile() {
    viewModelScope.launch(Dispatchers.IO) {
      try {
      } catch (e: Throwable) {

        // assign String resource id to profileLoadError
          when (e) {
            is NetworkError -> R.string.network_error
            else -> R.string.unknown_error

      } finally {

And my error text view XML looks like the following.

  android:text="@{viewModel.profileLoadError ?? ``}"
  android:visibility="@{viewModel.profileLoadError != null ? View.VISIBLE : View.GONE}" />

But I am getting a class cast exception when I try to run it.

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.CharSequence

If I remove the null coalescing operator from the data binding expression like the following, I get a resource not found exception.

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0

I know that it is kind of an idiotic question, but how do I fix this?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

is NetworkError -> R.string.network_error ->in this line you are returning String to post value for profileLoadError of type Int.That’s why you got ClassCastException.

So change the type of LiveData to String

val profileLoadError = MutableLiveData()

Solution 2

After the suggestion from @androidLearner, I created a binding adapter for the android:text attribute.

fun TextView.setText(@StringRes resId: Int?) {
  resId ?: return
  if (resId == ResourcesCompat.ID_NULL) {
    text = ""
  } else {

Now, I can pass the string id without worrying about its nullability.


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply