1

What I'm already having:

This is my ImageView:

<ImageView
        android:id="@+id/play_pause"
        android:layout_width="match_parent"
        android:layout_height="@dimen/timer_button_layoutHeight"
        app:srcCompat="@drawable/play_button_vector"
        android:onClick="startOrPauseTimer"/>

I'm using Android plugin version 2.0:

classpath 'com.android.tools.build:gradle:2.0.0'

I have the vector drawable support library flag enabled in the defaultConfig of android in the app level build.gradle:

vectorDrawables.useSupportLibrary = true

I have also added the vector drawable support library dependency:

compile 'com.android.support:support-vector-drawable:23.2.1'

In the Activity class, I'm dynamically changing the vector drawable based on click listeners:

playPauseButton = (ImageView) findViewById(R.id.play_pause);
playPauseButton.setImageResource(R.drawable.play_button_vector);

The problem:

I'm getting Resource not found exception in pre-lollipop devices. The code works fine for lollipop onwards.

android.content.res.Resources$NotFoundException: File res/drawable/play_button_vector.xml from drawable resource ID #0x7f020119

I can see nothing at the place where the image should be. However, if I click on that place, where the image is supposed to be, it gets caught by the clickListener. What am I doing wrong?

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
  • You are converting any `SVG` to vector ? – Janki Gadhiya Apr 18 '16 at 09:06
  • yeah, I received the SVG file and I then imported it as a vector drawable. – Akeshwar Jha Apr 18 '16 at 09:07
  • Try using default vector instead. Doing from SVG gives error in my case too. For default vector it will work without all the above stuff you tried. – Janki Gadhiya Apr 18 '16 at 09:10
  • Okay, I will try and update. Any thoughts on why the SVG gives error, but not the default vector? – Akeshwar Jha Apr 18 '16 at 09:18
  • I am also the victim of that issue. :D. for Gradle Plugin 2.0+ and for Gradle Plugin 1.5 you need to add --> android { defaultConfig { generatedDensities = [] } // This is handled for you by the 2.0+ Gradle Plugin aaptOptions { additionalParameters "--no-version-vectors" } } – Janki Gadhiya Apr 18 '16 at 09:19
  • Possible duplicate of [Android vector compatibility](http://stackoverflow.com/questions/31870992/android-vector-compatibility) – AdamHurwitz Jul 21 '16 at 20:47

3 Answers3

1

According to a recent post from Android Developers,

For AppCompat users, 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 https://code.google.com/p/android/issues/detail?id=205236. Using app:srcCompat and setImageResource() continues to work.

Source : https://plus.google.com/+AndroidDevelopers/posts/iTDmFiGrVne

NSimon
  • 5,212
  • 2
  • 22
  • 36
1

Try this instead of ContextCompat.getDrawable().

Drawable d = AppCompatDrawableManager.get().getDrawable(context, resID);
Embydextrous
  • 1,611
  • 1
  • 12
  • 20
0

I have tried all solutions mentioned above. Nothing worked for me. The only thing worked for me is very simple is to update all support libraries to latest version as this bug has been fixed in it. So i simple did the below thing in build that gradle updated SDK and support to 25.

android {
    compileSdkVersion 25
    buildToolsVersion "25"
  defaultConfig {
        targetSdkVersion 25
   }

}
In dependencies 

    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
Googler
  • 211
  • 1
  • 3
  • 12