14

我正在尝试为自定义实现选择器可绘制资源ArrayAdapter。我一直在得到一个android.content.res.Resources$NotFoundException: File res/drawable/list_selector.xml from drawable resource ID #0x7f020

有人可以提供建议吗?

我有两个 xml 文件:

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>

资源/布局/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>

最后,我的代码按如下方式加载列表项:

    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;
    }
4

5 回答 5

13

看看@Nebraska 的建议是否有帮助。

否则,试试这个:

<?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>
于 2013-07-29T23:30:26.530 回答
4

首先,将它们放在hdpi文件夹中,然后清理项目。它将更新所有内容,并且错误应该消失了。

于 2013-07-29T23:21:35.797 回答
0

我遇到了类似的问题,我能够通过删除 .xml 可绘制对象顶部的空行来修复它。

于 2020-05-12T15:40:38.867 回答
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...

于 2018-12-06T20:40:06.507 回答
0

另一种解决方案是我将 XML drawables 移动到 drawable/ 文件夹,它对我来说非常好用。你可能想试一试。

KitKat 及以下版本的 VectorDrawableCompat Resources$NotFoundException

于 2017-04-05T14:31:10.590 回答