1

I am starting with android and I want to add a border to cells as described in this answer. So I created my cell_background.xml file, which Eclipse created in res\drawable and that contains

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

Having read that there are several issues with the drawable folder, I copied it verbatim into the res\drawable-*dpi directories

Now, my app crashes in the following line

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

with this exception

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

Both the project and the emulator are set to v3.0

Any ideas? I have already cleaned and rebuilt the project but it still crashes.

Community
  • 1
  • 1
SJuan76
  • 24,532
  • 6
  • 47
  • 87

3 Answers3

7

The problem is that you use Resources.getSystem(), which will give you a reference to the system resources. You should use context.getResources() instead.

David Burström
  • 1,592
  • 14
  • 14
  • Ok both you and ud_an were right, accepting your answer because you were quicker and also explained better my mistake. – SJuan76 Dec 16 '11 at 14:47
2

Try with the following code to check whether the resource exists or not

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());
        }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
1

not sure about issue regarding putting in Drawable folder only i haven't got issue any, Still try using this way tht i use generally

Drawable drawable = getResources().getDrawable(R.drawable.cell_background);
ud_an
  • 4,939
  • 4
  • 27
  • 43