Support library VectorDrawable Resources$NotFoundException

前端 未结 14 1291
你的背包
你的背包 2020-11-29 18:40

I am using Design Support Library version 23.4.0. I have enabled the gradle flag:

defaultConfig {
    vectorDrawables.useSupportLibrary = tr         


        
相关标签:
14条回答
  • 2020-11-29 18:40

    change

    imageView.setImageResource(R.drawable.drawable_image)
    

    to

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

    if you want to use vectordrawable in xml, use this:

    app:srcCompat="@drawable/drawable_image"
    
    0 讨论(0)
  • 2020-11-29 18:41

    Try using:

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

    You don't have to add AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); this way.

    Just inflate your vector drawables using VectorDrawableCompat and you're all set.

    0 讨论(0)
  • 2020-11-29 18:43

    To complement some of the answers here: backward-compatible support for VectorDrawables comes with a price and doesn't work in all cases.

    In which cases does it work? I've made this diagram to help (valid for Support Library 23.4.0 to at least 25.1.0).

    0 讨论(0)
  • 2020-11-29 18:43

    I had the same problem and actually what was missing is I was using app:srcCompat on AppCompatTextView except of AppCompatImageView.

    The way I have found the problematic part:

    My error looks like:

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

    Here are the steps I followed the resource id of the mentioned drawable :

    • APK Analyzer -> classesXXX.dex
    • In this dex file I opened the directory of my apps package name and went to R$drawable file
    • R$drawable -> Show as byte code.
    • Search for ID [0x7f0700d1] (check your own ID)
    • Find the image and check for all the usages (CMD + F7) of the resource
    • Fix

    Hope it will help somebody.

    0 讨论(0)
  • 2020-11-29 18:47
    defaultConfig {
      vectorDrawables.useSupportLibrary = true
    }
    

    use this in app.gradle

    Then use AppCompatDrawableManager to setDrawable and getDrawable. Works for me

    0 讨论(0)
  • 2020-11-29 18:55

    In my particular case, I had this problem because I was using a drawable selector as the image resource with several vectors in the selector, as in:

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

    Yes, pretty bad, but didn't know better at the time.

    So, the right way of doing this is using the tint property in your vector file, as in:

    <vector ..vector properties..
            android:tint="@color/vector_color_selector">
                  <path ..path properties../>
    </vector>
    

    (You can also use the app:tint attribute in the AppCompatImageView)

    And now, your vector_color_selector file should have the colors you want, as in:

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

    I hope this helps someone if previous answers didn't work for you. Stating the obvious, but I must say that you still need to set vectorDrawables.useSupportLibrary = true in gradle, use AppCompatImageView and use app:srcCompat or setImageDrawable + AppCompatResources.getDrawable to avoid any troubles with the vector compat library.

    0 讨论(0)
提交回复
热议问题