Support library VectorDrawable Resources$NotFoundE

2019-01-30 10:20发布

I am using Design Support Library version 23.4.0. I have enabled the gradle flag:

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

I am using build tools version 23.0.2, but still, I am getting Resources$NotFoundException on KitKat or lower.

It is occurring when I use android:drawableLeft or imageView.setImageResource(R.drawable.drawable_image).

And yes, I am putting this on every activity where I am using drawables

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

Is this a bug of the support library?

12条回答
疯言疯语
2楼-- · 2019-01-30 11:06

To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases.

In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).

VectorDrawable cheatsheet

查看更多
爷、活的狠高调
3楼-- · 2019-01-30 11:09

Support for vector drawables in places like android:drawableLeft was disabled in support library 23.3. It was announced on Google+:

we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1. Using app:srcCompat and setImageResource() continues to work.

Links to issues:

However, if you can live with those issues, in 23.4 you can re-enable this functionality using AppCompatDelegate.setCompatVectorFromResourcesEnabled().

If you're curious how this works, the best person to learn from is Chris Banes, who authored this functionality. He explains in detail on his blog.

查看更多
放我归山
4楼-- · 2019-01-30 11:10

Try using:

imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));

You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this way.

Just inflate your vector drawables using VectorDrawableCompat and you're all set.

查看更多
乱世女痞
5楼-- · 2019-01-30 11:11
defaultConfig {
  vectorDrawables.useSupportLibrary = true
}

use this in app.gradle

Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me

查看更多
冷血范
6楼-- · 2019-01-30 11:14

Sorry for being late to the party but this answer may help users who want to enable the flag AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); for all activities.

1. Create a class which extends to Application (android.app.Application)

public class MyApplicationClass extends Application
{
    @Override
    public void onCreate()
    {
        super.onCreate();
    }
}

2. Head over to Manifest.xml and add the following line to your tag

<application
    android:name=".MyApplicationClass"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ...
</application>

3. Add the following code above onCreate in MyApplicationClass.java

// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

Complete code for MyApplicationClass.java

import android.app.Application;
import android.support.v7.app.AppCompatDelegate;

/**
* Created by Gaurav Lonkar on 23-Dec-17.
*/

public class MyApplicationClass extends Application
{
    // This flag should be set to true to enable VectorDrawable support for API < 21
    static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }
}
查看更多
地球回转人心会变
7楼-- · 2019-01-30 11:15

change

imageView.setImageResource(R.drawable.drawable_image)

to

imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));

if you want to use vectordrawable in xml, use this:

app:srcCompat="@drawable/drawable_image"
查看更多
登录 后发表回答