支持库VectorDrawable资源$NotFoundException

74

我正在使用Design Support Library版本23.4.0。我已启用Gradle标志:

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

我正在使用版本为23.0.2的构建工具,但在KitKat或更低版本上仍然遇到Resources$NotFoundException错误。

当我使用android:drawableLeftimageView.setImageResource(R.drawable.drawable_image)时会出现这种情况。

是的,我在每个使用可绘制对象的活动中都进行了设置。

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

这是支持库的一个漏洞吗?


3
你好,我和你一样遇到了同样的问题。你有解决 Android:drawableLeft 的方法吗? - Sayem
1
@Sayem 不行。我必须完全从按钮中删除图形。但是你可以提供单独的PNG作为可绘制对象。 - Arka
是的,我已经按照您的建议完成了它。 - Sayem
14个回答

1

API 16 animation
解析Drawable

在此支持库中,可以通过以下方式解析`VectorDrawable``AnimatedVectorDrawable`:

  • 调用静态的getDrawable()方法:
//这将只会解析一个根元素为<vector>的可绘制对象
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);
//这将只会解析一个根元素为<animated-vector>的可绘制对象 AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);
// 这将解析任何可绘制对象,并且在api 21+设备上自动回退到lollipop实现 ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

如果在Java代码中解析Drawable,则建议始终使用ResourcesCompat.getDrawable(),因为它处理了Lollipop回退(如果适用)。这允许系统缓存Drawable ConstantState,因此更有效率。
该库具有以下变形(双向)动画:

  • 播放-暂停形变动画
  • 播放-停止形变动画
  • 箭头-汉堡菜单形变动画

  • 如您所见,我在我的API 16手机上生成了上述图像:

    import com.wnafee.vector.compat.AnimatedVectorDrawable;
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
    

    请查看 vector-compat 的 github README,链接在这里:https://github.com/wnafee/vector-compat
    如果你将其与应用程序模块的 build.gradle dependencies(通常位于文件末尾)合并,它将解决您的问题(至 API 14)。

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:design:25.0.0'
    //not needed
    //  compile 'com.android.support:support-vector-drawable:25.0.0'
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
    //  Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
    //not needed
    //  compile 'com.android.support:support-animated-vector-drawable:25.0.0'
    }
    

    1
    在我的情况下,我遇到了这个问题,因为我正在使用可绘制选择器作为图像资源,并在选择器中使用了多个矢量图像,如下所示:
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/vector_select_blue"/>
        <item android:state_pressed="true" android:drawable="@drawable/vector_select_black"/>
        .
        .
        etc
    </selector>
    

    是的,很糟糕,但当时不知道更好的方法。
    因此,正确的做法是在您的矢量文件中使用色调属性,如下所示:
    <vector ..vector properties..
            android:tint="@color/vector_color_selector">
                  <path ..path properties../>
    </vector>
    

    你也可以在AppCompatImageView中使用app:tint属性。

    现在,你的vector_color_selector文件应该包含你想要的颜色,如下所示:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="@color/blue"/>
        <item android:state_pressed="true" android:color="@color/black"/>
        .
        .
        etc
    </selector>
    

    如果之前的回答对您没有帮助,我希望这篇文章能够帮到您。显而易见的是,您仍然需要在gradle中设置vectorDrawables.useSupportLibrary = true,并使用AppCompatImageView和app:srcCompat或setImageDrawable + AppCompatResources.getDrawable来避免与矢量图兼容库产生任何问题。


    0

    在评论中,Harish Gyanani建议使用AppCompatImageView代替ImageView,我用这个方法可以正常工作。

    官方文档


    0

    我遇到了同样的问题,实际上缺少的是我在 AppCompatTextView 上使用了 app:srcCompat 而不是 AppCompatImageView。

    我发现问题所在的方法:

    我的错误看起来像:

    Fatal Exception: android.content.res.Resources$NotFoundException
    Resource ID #0x7f0700d1
    

    以下是我按照所提到的drawable资源ID所遵循的步骤:

    • APK分析器 -> classesXXX.dex
    • 在该dex文件中,我打开了我的应用程序包名称的目录,并进入了R $ drawable文件
    • R $ drawable -> 显示为字节码。
    • 搜索ID [0x7f0700d1](请检查您自己的ID)
    • 查找图像并检查资源的所有用法(CMD + F7)
    • 修复

    希望能对某些人有所帮助。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接