2011-12-16 106 views
1

我開始與Android,我想要添加一個邊框到單元格描述in this answer。所以,我創建了cell_background.xml文件,Eclipse的創造res\drawable,幷包含NotFoundException當尋找可繪製資源

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape= "rectangle" > 
    <solid android:color="#000"/> 
    <stroke android:width="1dp" android:color="#ff9"/> 
</shape> 

看了有與繪製文件夾的幾個問題,我複製它逐字到res\drawable-*dpi目錄

現在,在下面的行

Drawable drawable = Resources.getSystem().getDrawable(R.drawable.cell_background); 

我的應用程序崩潰與此異常

12-16 14:26:28.624: E/AndroidRuntime(533): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f020000 

項目和模擬器都設置爲v3.0

任何想法?我已經清理並重建了該項目,但仍然崩潰。

+0

嘗試清洗項目 – 2011-12-16 14:39:33

+0

已經完成,但由於 – SJuan76 2011-12-16 14:40:40

回答

7

的問題是,你使用Resources.getSystem(使用)這會給你一個系統資源的參考。你應該使用context.getResources()來代替。

1

不知道關於投入可繪製文件夾的問題只是我沒有得到任何的問題,還是用這種方式嘗試謂我一般

Drawable drawable = getResources().getDrawable(R.drawable.cell_background); 
2

用下面的代碼嘗試檢查資源是否存在

int drawRes = getDrawableResourceID(context, "cell_background")); 
if(drawRes>0){ 
getResources().getDrawable(drawRes); 
} 

//To detect whether the reource exits in drawable or not 
    public static int getDrawableResourceID(Context context, 
       String identifierName) { 

      return context.getResources().getIdentifier(identifierName, 
        "drawable", context.getPackageName()); 
     } 
相關問題