支持库VectorDrawable Resources $ NotFoundException

时间:2016-06-03 13:17:37

标签: android android-support-library androiddesignsupport android-vectordrawable

我使用的是设计支持库版本 23.4.0 。我启用了gradle标志:

Set oFSO = CreateObject("Scripting.FileSystemObject")
oStartFolder = "C:/Documents"
Set oFolder = oFSO.GetFolder(oStartFolder)
oFSO.GetFolder (oFolder)
For Each FileItem In oFolder.Files
if Instr(FileItem,".csv") Then Call ImportCSV(FileName) 'you would change the above code to don't ask folder and set the argument so each time you call it would be the file csv in the folder
Next FileItem 

我正在使用构建工具版本 23.0.2 ,但我仍然在KitKat或更低版本上获得defaultConfig { vectorDrawables.useSupportLibrary = true }

当我使用Resources$NotFoundExceptionandroid:drawableLeft时会发生这种情况。

是的,我把它放在我使用drawables的每个活动上

imageView.setImageResource(R.drawable.drawable_image)

这是支持库的错误吗?

14 个答案:

答案 0 :(得分:71)

使用支持库23.4.0:

让我使用了3个单独的东西
  1. 将此添加到build.gradle

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    
  2. 将以下内容添加到Application类的onCreate

    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    
  3. 对于您在其中设置vector drawable replace的所有xml视图

    android:src
    

    app:srcCompat
    

    并在代码中替换为:

    imageView.setImageResource(...);
    

    imageView.setImageDrawable(...);
    

答案 1 :(得分:53)

为了补充这里的一些答案:对VectorDrawables的向后兼容支持需要付出代价,并且在所有情况下都不起作用

在哪些情况下有效?我已提供this diagram帮助(对支持库23.4.0有效至​​少25.1.0)。

VectorDrawable cheatsheet

答案 2 :(得分:29)

尝试使用:

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

您不必添加AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 这样。

只需使用VectorDrawableCompat为你的矢量绘图充气,你就可以了。

答案 3 :(得分:25)

我们遇到了同样的问题。在Kitkat上看不到矢量drawables。我通过将AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);添加到活动的onCreate方法来解决了这个问题。

在此之前不要忘记添加:

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

并为使用矢量drawable的视图调用setImageResource。我的观点是ImageButton。我有Android SDK构建工具版本23.0.3

答案 4 :(得分:12)

很抱歉迟到了,但这个答案可能会帮助那些想要为所有活动启用 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 标志的用户。

<强> 1。创建一个扩展到Application(android.app.Application)

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

<强> 2。转到Manifest.xml并在标记中添加以下行

<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。在MyApplicationClass.java

中的onCreate上面添加以下代码
// This flag should be set to true to enable VectorDrawable support for API < 21
static
{
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

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();
    }
}

答案 5 :(得分:8)

defaultConfig {
  vectorDrawables.useSupportLibrary = true
}

在app.gradle中使用它

然后使用AppCompatDrawableManager来设置setDrawable和getDrawable。适合我

答案 6 :(得分:7)

在支持库23.3中禁用了android:drawableLeft等地方对矢量绘图的支持。它是在Google+宣布的:

  

我们决定删除让您使用vector的功能   由于发现问题,可从前Lollipop设备上的资源中提取   在版本23.2.0 / 23.2.1中的实现。使用app:srcCompat和setImageResource()   继续工作。

问题链接:

但是,如果您能解决这些问题,请在23.4中使用AppCompatDelegate.setCompatVectorFromResourcesEnabled()重新启用此功能。

如果你很好奇这是如何运作的,那么最好的学习对象是Chris Banes,他是这个功能的创作者。他详细解释了on his blog

答案 7 :(得分:4)

更改

imageView.setImageResource(R.drawable.drawable_image)

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

如果你想在xml中使用vectordrawable,请使用:

app:srcCompat="@drawable/drawable_image"

答案 8 :(得分:1)

很久以前我遇到过类似的问题,设置

后无法解决问题

vectorDrawables.useSupportLibrary = true

仅在我创建“mipmap”文件夹时使用,并且使用了代码

imageView.setImageResource (R.mipmap.drawable_image)

它有更多信息here

答案 9 :(得分:1)

API 16 animation
膨胀Drawable

此支持库中的

`VectorDrawable``AnimatedVectorDrawable`可以通过这种方式充气:

  • 调用静态getDrawable()方法:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);

//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

如果在java代码中膨胀Drawable,建议始终使用ResourcesCompat.getDrawable(),因为这会在适用时处理Lollipop后备。这允许系统缓存Drawable ConstantState,因此效率更高。
该库具有以下变形(双向​​)动画:

  • 播放 - 暂停变形动画
  • 播放 - 停止变形动画
  • Arrow-Hamburger菜单变形动画

  • 如您所见,我在API 16电话上制作了上述图片:

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

    在此处查看vector-compat的github 自述文件https://github.com/wnafee/vector-compat
    如果您将其与您的应用模块的API 14 build.gradle合并(通常位于文件末尾),这将解决您的问题(降至dependencies):

    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'
    }
    

    答案 10 :(得分:1)

    请勿将您的矢量放入drawable-anydpi ,旧设备不支持

    将它们放入drawable

    答案 11 :(得分:0)

    使用AppCompatImageView代替Harish Gyanani在评论中说的ImageView,对我来说很好用。

    Official docs

    答案 12 :(得分:0)

    在我的特定情况下,我遇到了这个问题,因为我将可绘制选择器用作图像资源,并且选择器中有多个矢量,如:

    <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>
    

    是的,很糟糕,但当时还不了解。

    因此,正确的方法是使用矢量文件中的tint属性,如下所示:

    <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来避免矢量兼容库的任何麻烦。

    答案 13 :(得分: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)
    • 修复

    希望它将对某人有所帮助。