2011-12-16 103 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()); 
     } 
相关问题