2013-07-29 49 views
5

我試圖爲自定義ArrayAdapter實現選擇器可繪製資源。我始終得到android.content.res.Resources$NotFoundException: File res/drawable/list_selector.xml from drawable resource ID #0x7f020未找到可繪製資源

任何人都可以提供建議嗎?

我有兩個XML文件:

RES /繪製/ 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 /佈局/ 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; 
    } 

回答

8

看看@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> 
+0

謝謝,drawable做到了。請刪除邏輯建議,它不適用於該問題。 – Constantin

+0

@Constantin完成。 – Vikram

+0

任何想法如何選擇器順便工作?我在運行時玩弄改變ListView項目的背景顏色。一種方法是以編程方式使用'setBackgroundColor()',這很容易並且有效。選擇器(意外)也有效,但不正確。它沒有設置正確的顏色。關於通貨膨脹的看法,換句話說,選擇者只能在通脹或持續時間內靜態使用一次?或者使用默認的'getView()'調用來利用它們? – Constantin

1

首先,把它們放在hdpi文件夾中,然後清理項目。它會更新一切,錯誤應該消失。

+0

不幸的是,這並沒有奏效。清理該項目,卸載應用程序,同樣的錯誤。 – Constantin

+0

那些android:顏色的東西可能會有問題 – Nebraska

+0

我會給你一個鏡頭。 – Constantin