Android: getting Resources$NotFoundException for abc_ic_ab_back_material

Jon picture Jon · Jul 19, 2016 · Viewed 11.2k times · Source

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)

Answer

Jon picture Jon · Jul 19, 2016

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