android – Can't retrieve resources programmatically using Kotlin-ThrowExceptions

Exception or error:

I was trying to load a drawable programmatically in my kotlin app via


and all I got was:

Method threw 'android.content.res.Resources$NotFoundException' exception.

whatever the target drawable was.

After trying different things, I finally tried on a newly created Kotlin project, only to find out it didn’t work either.
I then created a new Java project, and everything worked flawlessly in this one.

I found nothing about people having the same problem online, I can get the “Resources” object in both project, but it just can’t find drawables in the Kotlin one.

Is there anything to do to make it work that I don’t know about?

I’m getting the resources like this in the newly created project:

override fun onResume() {

    val drawable = resources.getDrawable(R.drawable.ic_launcher_foreground)

This happens whatever the target resource is, drawables, mipmaps, colors…

I’m on Android Studio 3.5.3 with gradle 3.5.3 and Kotlin 1.3.61, API level 26

Project resources are the one added on project creation

How to solve:

I found a temporary fix, and mostly something very interesting.
Taking the example of “ic_launcher_background” as a random drawable, Java gives this result :

enter image description here

While the exact equivalent code in Kotlin gives us this :
enter image description here

I don’t understand why, but when using a project generated the Kotlin way, the Ids retrievable via R just don’t match the ones used by the Resources class.

So for now I’ll use the “getIdentifier()” method to fix my problem, but it really isn”t something you’d want to do naturally.

If someone that has a better understanding of what is going on here could unfold this mystery, that would be greatly appreciated.


Try this:

override fun onResume() {

        val drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher_foreground, null)

Leave a Reply

Your email address will not be published. Required fields are marked *