【问题标题】:NotFoundException when looking for Drawable resource查找可绘制资源时出现 NotFoundException
【发布时间】:2011-12-16 23:24:38
【问题描述】:

我从 android 开始,我想为单元格添加边框,如 in this answer 所述。所以我创建了我的 cell_background.xml 文件,Eclipse 在res\drawable 中创建了该文件,其中包含

<?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

有什么想法吗?我已经清理并重建了项目,但它仍然崩溃。

【问题讨论】:

  • 已经完成了,谢谢

标签: android


【解决方案1】:

问题在于您使用了 Resources.getSystem(),它将为您提供对系统资源的引用。您应该改用 context.getResources()。

【讨论】:

  • 好的,你和 ud_an 都是对的,接受你的回答,因为你更快,也更好地解释了我的错误。
【解决方案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());
        }

【讨论】:

    【解决方案3】:

    不确定关于放入 Drawable 文件夹的问题,只是我没有任何问题,仍然尝试使用我通常使用的这种方式

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2019-04-29
      • 2021-01-06
      相关资源
      最近更新 更多