棒棒糖前设备的 ResourceNotFoundException

ResourcesNotFoundException for pre-lollipop devices

我已经拥有的东西:

这是我的 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"/>

我正在使用 Android 插件版本 2.0:

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

我在应用级别 android 的默认配置中启用了矢量可绘制支持库标志 build.gradle:

vectorDrawables.useSupportLibrary = true

我还添加了矢量可绘制支持库依赖项:

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

在 Activity class 中,我正在根据点击侦听器动态更改矢量可绘制对象:

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

问题:

我在 pre-lollipop 设备中遇到资源未找到异常。该代码适用于棒棒糖以后的版本。

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

我在应该是图像的地方什么也看不到。但是,如果我单击该图像应该位于的位置,它就会被 clickListener 捕获。我做错了什么?

根据 Android 开发人员最近 post 的说法,

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.

来源:https://plus.google.com/+AndroidDevelopers/posts/iTDmFiGrVne

试试这个而不是 ContextCompat.getDrawable()。

Drawable d = AppCompatDrawableManager.get().getDrawable(context, resID);

我已经尝试了上面提到的所有解决方案。没有什么对我有用。唯一对我有用的事情非常简单,就是将所有支持库更新到最新版本,因为这个错误已在其中得到修复。所以我简单地在构建中做了以下事情 gradle 更新了 SDK 并支持到 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'