Jon
Jon

Reputation: 8021

Android: getting Resources$NotFoundException for abc_ic_ab_back_material

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)

Upvotes: 10

Views: 12349

Answers (7)

Ahmed Esameddeen
Ahmed Esameddeen

Reputation: 89

Instead of:

ContextCompat.getdrawable()

Try using:

AppCompatDrawableManager.get().getDrawable() 

Upvotes: 0

Samik
Samik

Reputation: 890

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.

Upvotes: 1

Leo DroidCoder
Leo DroidCoder

Reputation: 15046

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)

Upvotes: 1

Roy Solberg
Roy Solberg

Reputation: 19683

I was using the application context when calling ContextCompat.getDrawable() which also crashes the app with Resources$NotFoundException and now the following message even though everything else was set up just fine:

If the resource you are trying to use is a vector resource, you may be referencing it in an unsupported way. See AppCompatDelegate.setCompatVectorFromResourcesEnabled() for more info.

All I had to do was to change to the view's Context. :)

Upvotes: 0

umi
umi

Reputation: 157

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.

Upvotes: 2

Jon
Jon

Reputation: 8021

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

Upvotes: 23

Fred Grott
Fred Grott

Reputation: 3476

private resources its treated differently at compile time and runtime..to fix I usually take the offending private resource and backport it to my app in my res files

Upvotes: 0

Related Questions