SJuan76
SJuan76

Reputation: 24895

NotFoundException when looking for Drawable resource

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.

Upvotes: 1

Views: 4957

Answers (3)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53687

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());
        }

Upvotes: 2

David Burstr&#246;m
David Burstr&#246;m

Reputation: 1610

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

Upvotes: 7

ud_an
ud_an

Reputation: 4921

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);

Upvotes: 1

Related Questions