Android: getting Resources$NotFoundException for abc_ic_ab_back_material

坚强是说给别人听的谎言 提交于 2019-11-29 09:52:40

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

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.

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)

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.

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

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

try using AppCompatDrawableManager.get().getDrawable() instead of ContextCompat.getdrawable()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!