2

I found there were numerous vector drawable crashes happened on pre-Lollipop devices and I can’t reproduce it.

One is caused by org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector when I use setImageResource

android.content.res.Resources$NotFoundException: File res/drawable/ic_mylib_offline_tracks_32.xml from drawable resource ID #0x7f020414
        at android.content.res.Resources.loadDrawable(Resources.java:2141)
        at android.content.res.Resources.getDrawable(Resources.java:711)
        at android.support.v7.widget.VectorEnabledTintResources.superGetDrawable(VectorEnabledTintResources.java:74)
        at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:433)
        at android.support.v7.widget.VectorEnabledTintResources.getDrawable(VectorEnabledTintResources.java:67)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:360)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
        at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85)
        at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
...

Code:

@override
protected void onBindItemViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    ...    
    viewHoler.imageView.setImageResource(vectorResId);
    ...
}

Vector drawable - ic_mylib_offline_tracks_32.xml:

<vector android:height="24dp" android:viewportHeight="32.0"
    android:viewportWidth="32.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#00000000" android:fillType="evenOdd"
        android:pathData="M7,21v3.75c0,0.69 0.54,1.25 1.2,1.25h15.6c0.66,0 1.2,-0.56 1.2,-1.25V21M19,17l-3,2 -3,-2M16,6v12"
        android:strokeColor="#A0A098" android:strokeLineCap="round"
        android:strokeLineJoin="round" android:strokeWidth="1"/>
</vector>

And the others caused by android.content.res.Resources$NotFoundException: File res/drawable/ic_more_white_24.xml from drawable resource ID #0x7f020357. 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. when I refresh toolbar menu visibility.

android.content.res.Resources$NotFoundException: File res/drawable/ic_more_white_24.xml from drawable resource ID #0x7f020357. 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.
        at android.content.res.Resources.loadDrawable(Resources.java:2096)
        at android.content.res.Resources.getDrawable(Resources.java:700)
        at android.support.v7.widget.VectorEnabledTintResources.superGetDrawable(VectorEnabledTintResources.java:74)
        at android.support.v7.widget.AppCompatDrawableManager.onDrawableLoadedFromResources(AppCompatDrawableManager.java:435)
        at android.support.v7.widget.VectorEnabledTintResources.getDrawable(VectorEnabledTintResources.java:67)
        at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:353)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
        at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:491)
        at android.support.v7.view.menu.ActionMenuItemView.initialize(ActionMenuItemView.java:125)
        at android.support.v7.widget.ActionMenuPresenter.bindItemView(ActionMenuPresenter.java:210)
        at android.support.v7.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:187)
        at android.support.v7.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:196)
        at android.support.v7.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:476)
        at android.support.v7.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1164)
        at android.support.v7.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:95)
        at android.support.v7.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:229)
        at android.support.v7.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:291)
        at android.support.v7.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:1051)
        at android.support.v7.view.menu.MenuBuilder.onItemVisibleChanged(MenuBuilder.java:1090)
        at android.support.v7.view.menu.MenuItemImpl.setVisible(MenuItemImpl.java:651)
...

Code:

public void refreshMenuItemVisibility(boolean visibility) {
    toolbar.getMenu().findItem(R.id.menu_overflow).setVisible(visible);
}

Vector drawable - ic_more_white_24.xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="32dp"
    android:height="32dp"
    android:viewportWidth="32.0"
    android:viewportHeight="32.0">
    <path
        android:pathData="M10.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
        android:fillType="evenOdd"
        android:fillColor="#FFFFFF"/>
    <path
        android:pathData="M16.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
        android:fillType="evenOdd"
        android:fillColor="#FFFFFF"/>
    <path
        android:pathData="M22.5,16.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"
        android:fillType="evenOdd"
        android:fillColor="#FFFFFF"/>
</vector>

I have already followed the step from the official document, but it doesn't work.

vectorDrawables.useSupportLibrary = true


public class MainActivity extends AppCompatActivity {
    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
}

My library and tools version are as follow:

  • Android Support Library: 26.0.0
  • Build Tools: 26.0.0
  • Gradle: 3.3
  • Android Plugin: 2.3.3

UPDATE

All of the vectors are put in drawable folder.

Community
  • 1
  • 1
Han
  • 61
  • 1
  • 6
  • See [this SO answer](https://stackoverflow.com/questions/35806538/animatedvectordrawable-in-support-library-and-animation-of-pathdata/41409624#41409624) – Jon Goodwin Apr 27 '18 at 11:26
  • where is ic_more_white_24.xml file please show it. – krishank Tripathi Apr 27 '18 at 11:35
  • @Han.... does my answer helped you or not? – letsintegreat Apr 27 '18 at 11:42
  • @krishankTripathi Sorry I forgot to write down the file name of these two vectors. And now you can see it. – Han Apr 27 '18 at 17:25
  • 1
    I do have the exact same problem. Spent a few hours on stackoverflow without success so far. I'm thinking maybe this happens if the device is super low on memory. The support lib fails to decode the vector xml and fallbacks to the native decode which fails with an exception. Really just a wild guess but I have no clue what else could be going on. – mbonnin May 17 '18 at 17:07

2 Answers2

4

Try this and see if working or not.

For me its working as i am using support library 23.4.0:

Add this to build.gradle

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

Add the following to onCreate of your Application class

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

For all xml views in which you are setting a vector drawable replace

android:src

with

app:srcCompat

and in the code replace this:

imageView.setImageResource(...);

with

imageView.setImageDrawable(...);
krishank Tripathi
  • 616
  • 1
  • 7
  • 23
-1

There are couple of things you need to fix.

1) First put your vector image in drawable folder instead of drawable-v24.

2) Also add support for vector drawables in gradle file.

android {
     defaultConfig {

        vectorDrawables.useSupportLibrary = true
    }

3) There is also a work around for this. Make a resource file in your drawable folder and do something like this:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/Vector drawable"/>
</layer-list>
Umair
  • 6,366
  • 15
  • 42
  • 50
  • Sorry for the late reply, all of the vectors are put in `drawable` folder. And I have added the `support` in gradle and the `layer-list` wrapper, but it didn't work. – Han May 03 '18 at 02:48