KitKat specific- android.content.res.Resources$NotFoundException: File res/drawable/list_selector_white.xml from drawable resource ID

前端 未结 3 1578
执笔经年
执笔经年 2020-12-18 05:13

I tried the answer here, but it didn\'t work for me. I am using an xml as background and getting exceptiion - Resources$NotFoundException: File res/drawable/list_selec

相关标签:
3条回答
  • 2020-12-18 05:53

    Basically no, it's a bug that was fixed in Lollipop. The suggestion (until you can minSdkVersion=21) is to create a different drawable XML file for each different color you need (and have that as a theme attribute, I assume).

    The duplicate question, answers and comments discuss this bug: How to reference colour attribute in drawable?

    0 讨论(0)
  • 2020-12-18 06:04

    I had the same problem. The problem is that you are using ?attr/ in your drawable. Replace that with a @color or @drawable and it should go away. This does unfortunately mean that you have to bundle a drawable for each of your themes, it is a lot cleaner in lollipop.

    Check out this commit of mine on the project I'm working on, it was to resolve exactly this problem.

    You could create a drawable-v21 folder and put a drawable under it in hopes of a good time when you can support v21+ but I have chosen to go the other route and bundle different color resources (something you can see in the main branch, I have teal and default green colors).

    0 讨论(0)
  • 2020-12-18 06:06

    I was finally able to fix the issue. I will post answer so that it may help future readers.

    The problem is that we can't give attr values to item and gradient. I got the issue fixed by taking different xml files for colors.

    I ended up using different versions of list_selector_white_1 and _2.xml to:

    <!-- Selector style for listrow -->
    <item android:drawable="@drawable/gradient_bg_white_1" android:state_pressed="false" android:state_selected="false"/>
    <item android:drawable="@drawable/gradient_bg_hover_1" android:state_pressed="true"/>
    <item android:drawable="@drawable/gradient_bg_hover_1" android:state_pressed="false" android:state_selected="true"/>
    

    Similarly, I changed gradient_bg_hover and gradient_bg_white to different versions.

    Finally, I created an attribute list_selector with format integer and used it in fragment xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="?attr/list_selector" >
        ....Other views here....
    

    I am waiting for the time when I can support API level >= 21 in my app, in order to remove all these hacks.

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