Android: getting Resources$NotFoundException for abc_ic_ab_back_material

11,762

Solution 1

The answer to this turned out to be buried at the bottom of this guide:

https://medium.com/@chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.xucjbsts0

It turns out that all you need to add this line in at the beginning of the activity that will use the resource:

static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

Solution 2

Please make sure you are using AppCompatActivity instead Activity. If you're using AppCompat's theme, then you also need to use it's Activity.

Solution 3

Accepted answer is not covered all cases. It will not work on Android 4.0.3/4.1.1/4.1.2 platform with 25.x.x support library. The right way to fix problem with abc_ic_ab_back_material.xml is to override homeAsUpIndicator attribute in your theme. For example, my theme is inherited from Theme.AppCompat.Light.NoActionBar. As for value of mentioned attribute, you can use @drawable/abc_ic_ab_back_mtrl_am_alpha or your custom 'back' drawable.

Solution 4

Another solution,
in addition to AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
is to wrap your vector drawable into another drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_your_vector"/>
</selector>

Might be useful when you use as a drawable for a TextView (i.e. DrawableLeft)

Share:
11,762
Jon
Author by

Jon

Android developer

Updated on June 09, 2022

Comments

  • Jon
    Jon over 1 year

    I'm getting a Resources$notfoundexception on older (pre-L) devices. I'm including the full stacktrace below.

    My version of the support library is the latest (24.1.0), and I've included in my gradle file the line:

    vectorDrawables.useSupportLibrary = true
    

    My base theme is a noactionbar theme - "Theme.AppCompat.Light.NoActionBar"

    The crash is happening on this line in my code where I reference the back arrow in a support-toolbar in order to later change it's color:

    @SuppressLint("PrivateResource") final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
    

    What could be the cause of this? This code works fine for all users with L or above.

    07-19 22:36:57.029 9330-9330/mypkg E/AndroidRuntime: FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to resume activity {mypkg/mypkg.activites.myActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
         at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
         at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
         at android.app.ActivityThread.access$600(ActivityThread.java:138)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:4929)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:511)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
         at dalvik.system.NativeStart.main(Native Method)
      Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_ic_ab_back_material.xml from drawable resource ID #0x7f020013
         at android.content.res.Resources.loadDrawable(Resources.java:1957)
         at android.content.res.Resources.getDrawable(Resources.java:673)
         at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)
         at mypkg.base.mymethod(myactivity.java:100)
         at mypkg.mymethod(myactivity.java:100)
         at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511)
         at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
         at android.app.Activity.performResume(Activity.java:5341)
         at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599)
         at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
         at android.app.ActivityThread.access$600(ActivityThread.java:138) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4929) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
         at dalvik.system.NativeStart.main(Native Method) 
      Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
         at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
         at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
         at android.content.res.Resources.loadDrawable(Resources.java:1954)
         at android.content.res.Resources.getDrawable(Resources.java:673) 
         at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354) 
         at mypkg/mymethod(myactivity.java:100) 
         at mypkg/mymethod(myactivity.java:100) 
         at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:511) 
         at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178) 
         at android.app.Activity.performResume(Activity.java:5341) 
         at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2599) 
         at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647) 
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) 
         at android.app.ActivityThread.access$600(ActivityThread.java:138) 
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
         at android.os.Handler.dispatchMessage(Handler.java:99) 
         at android.os.Looper.loop(Looper.java:137) 
         at android.app.ActivityThread.main(ActivityThread.java:4929) 
         at java.lang.reflect.Method.invokeNative(Native Method) 
         at java.lang.reflect.Method.invoke(Method.java:511) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
         at dalvik.system.NativeStart.main(Native Method)
    
  • Jon
    Jon over 7 years
    Thanks. I've tried to take the resource and create it in my drawables folder. It complains that the resource isn't found, so I changed the name to make sure it wouldn't try to reference the support library version - and it still complains that the resource isn't found.
  • Fred Grott
    Fred Grott over 7 years
    when you port it you have to change the namespace you are referring to as its no longer the android anmespace
  • Artem Mostyaev
    Artem Mostyaev over 7 years
    I can't make it work. Added the line you mentioned, but the application crashes anyway. The stacktrace is the same, as in your question.
  • Jon
    Jon over 7 years
    did you also add the useSupportLibrary line to the gradle file and are you using the latest support library version?
  • Artem Mostyaev
    Artem Mostyaev over 7 years
    Yes. I'm guessing, that the problem is because your solution works only for Activities, extending AppCompatActivty, but mine is PreferenceActivity.
  • Jon
    Jon over 7 years
  • Artem Mostyaev
    Artem Mostyaev over 7 years
    Thanks! Using AppCompatDelegate didn't solved crash, by using that I've added the normal toolbar and icon is not needed now.
  • cybergen
    cybergen about 7 years
    Additionally I had to move my xml drawables from the drawable-v21 folder to the drawable folder to make them visible to older API versions. With AppCompat versions before 23.2.0 this was the way to go, as PNGs were created at build time for older API versions.