Resources$NotFoundException when Resource exists
-
14-11-2019 - |
سؤال
I am trying to set a drawable to an ImageView
from inside a custom adapter: SimpleCursorAdapter
. This is the code I'm using:
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ImageView i = (ImageView) view.findViewById(R.id.icon);
switch(Integer.parseInt(cursor.getString(4))){
case 0:
Drawable drawable = null;
try {
drawable = Resources.getSystem().getDrawable(R.drawable.andro_icon);
} catch (NotFoundException e) {
e.printStackTrace();
}
i.setImageDrawable(drawable);
But this catches a Resources$NotFoundException
:
02-29 06:39:48.467: W/System.err(13511): android.content.res.Resources$NotFoundException: Resource ID #0x7f02000c
The resource R.drawable.andro_icon
exists, the code compiles without any problem. I tried cleaning and rebuilding the project and restarting eclipse. What's happening here?
المحلول
You're calling Resources.getSystem()
but you want application resources, not system resources. Instead do:
context.getResources().getDrawable(R.drawable.andro_icon);
لا تنتمي إلى StackOverflow