Android: getting Resources$NotFoundException for abc_ic_ab_back_material
C

7

10

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)
Cabal answered 19/7, 2016 at 20:14 Comment(0)
C
23

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);
    }
Cabal answered 19/7, 2016 at 20:48 Comment(6)
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.Proctology
did you also add the useSupportLibrary line to the gradle file and are you using the latest support library version?Cabal
Yes. I'm guessing, that the problem is because your solution works only for Activities, extending AppCompatActivty, but mine is PreferenceActivity.Proctology
try looking at this: #17849693Cabal
Thanks! Using AppCompatDelegate didn't solved crash, by using that I've added the normal toolbar and icon is not needed now.Proctology
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.Plauen
P
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.

Proselytize answered 23/11, 2016 at 5:50 Comment(0)
J
1

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)

Jolo answered 28/8, 2017 at 9:28 Comment(0)
C
1

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.

Canica answered 18/10, 2017 at 16:51 Comment(0)
F
0

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

Fiddlededee answered 19/7, 2016 at 20:19 Comment(2)
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.Cabal
when you port it you have to change the namespace you are referring to as its no longer the android anmespaceFiddlededee
I
0

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. :)

Impearl answered 6/7, 2017 at 21:8 Comment(0)
V
0

Instead of:

ContextCompat.getdrawable()

Try using:

AppCompatDrawableManager.get().getDrawable() 
Vitriolic answered 25/6, 2019 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.