15

All I am attempting to implement a selector drawable resource for a custom ArrayAdapter. I am consistently getting a android.content.res.Resources$NotFoundException: File res/drawable/list_selector.xml from drawable resource ID #0x7f020

Could anyone offer suggestions?

I have two xml files:

res/drawable/list_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:color="@android:color/white" />
    <item android:state_checked="false" android:color="@android:color/black" />
</selector>

res/layout/list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/casualty_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/list_selector"/>

</LinearLayout>

Finally, my code loads the list item as follows:

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        TCCC cur_object = getItem(position);

        View cur_view = convertView;
        if(cur_view == null)
        {
            System.out.println("Inflating!");
            cur_view = View.inflate(getContext(), R.layout.list_item, null);
            String text_value = (cur_object.m_name.length() == 0) ? "New Casualty" : cur_object.m_name;

            TextView name_box = (TextView)cur_view.findViewById(R.id.casualty_text_view);
            if(name_box != null)
                name_box.setText(text_value);
        }

        return cur_view;
    }
Constantin
  • 16,812
  • 9
  • 34
  • 52

5 Answers5

15

See if what @Nebraska has suggested helps.

Else, try this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@android:color/white" />
    <item android:state_checked="false" android:drawable="@android:color/black" />
</selector>
Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thank you, the drawable did it. Please remove the logic suggestion, it is not applicable to the question. – Constantin Jul 29 '13 at 23:34
  • Any idea how selectors work btw? I am playing around with changing the background color of a ListView item at run time. One approach was to use `setBackgroundColor()` programmtically, this was easy and worked. The selector (unexpectedly) also works but not correctly. It's not setting the correct colors. Any thoughts on where I can read on view inflation, in other words are selectors only use statically once at inflation or continuously? Or do default `getView()` calls take advantage of them? – Constantin Jul 29 '13 at 23:38
  • @Constantin Could you define what you are looking for? In what states(or rather what combination of states) do you want the list item to change color? – Vikram Jul 29 '13 at 23:46
  • Using `setItemChecked` and `getCheckedItemPosition` with a `setChoiceMode(single)`, I can programmatically set the background color of ListView items in my subclassed `ArrayAdapter::getView` function. I was interested in achieving the same results using xml files and selectors, in hopes of improving efficiency. – Constantin Jul 29 '13 at 23:51
  • You can use states defined here [StateListDrawable](http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html). **Edit**: As for `setItemChecked()`, are you calling that on the TextView? – Vikram Jul 30 '13 at 00:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34410/discussion-between-vikram-and-constantin) – Vikram Jul 30 '13 at 00:02
  • A couple of links on inflating views: [Inflation is a Good Thing](http://www.androidguys.com/2008/07/09/inflation-is-a-good-thing/) _and_ [Android XML View inflation Tutorial](http://developerlife.com/tutorials/?p=303) – Vikram Jul 30 '13 at 01:41
  • 5
    Changing android:color to android:drawable in the selector did the trick. Thanks! – Luis Apr 03 '16 at 19:51
  • Luis comment is most valuable and accurate sentence in this whole topic... `DRAWABLE`, not `COLOR`, even when pointing on color... (but as far as I remember this was causing some issues < API14, currently not supporting such old devices) – snachmsm Feb 26 '20 at 08:45
4

First of all, put them in the hdpi folder, and then clean the project. It will update everything and the error should be gone.

verymessi
  • 117
  • 1
  • 12
0

An alternate solution is I moved the XML drawables to the drawable/ folder and it worked perfectly fine for me. You might want to give it a try.

VectorDrawableCompat Resources$NotFoundException on KitKat and below

Community
  • 1
  • 1
remykarem
  • 2,251
  • 22
  • 28
0

For me the confusion was that I had replaced all instances of a vector drawable with png, except for one, and this is what was failing. make sure and check all instances of the resource that is giving you the issue, and that all is as expected...

jacoballenwood
  • 2,787
  • 2
  • 24
  • 39
0

I encounter a similar issue, I was able to fix it by removing a blank line at top of a .xml drawable.