我收到此运行时错误
android.content.res.Resources$NotFoundException: Resource ID #0x0
我班上第38行:(Drawable res = context.getResources().getDrawable(imageResource);
)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View view = inflater.inflate(R.layout.breed, null);
Breed breed = breeds.get(position);
String breedName = breed.getNameString(context);
((TextView) view.findViewById(R.id.breedNameText)).setText(breedName);
String uri = "drawable/" + breedName.toLowerCase().replace(" ", "_");
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
ImageView image = (ImageView) view.findViewById(R.id.breedImage);
Drawable res = context.getResources().getDrawable(imageResource); // error occurs here
image.setImageDrawable(res);
return view;
}
}
我需要更改它,但我还需要将其更改为将我的图片更改为适当的图片,因为我使用此图片为不同的组拖动不同的图片。谁能告诉我怎么做?
答案 0 :(得分:0)
替换
String uri = "drawable/" + breedName.toLowerCase().replace(" ", "_");
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
使用
int imageResource = context.getResources().getIdentifier(breedName.toLowerCase().replace(" ", "_"), "drawable", context.getPackageName());
注意:
1. getIdentifier returns 0 if no such resource was found. (0 is not a valid resource ID.)
2. Also check in your R.java whether there is a drawable with the given breedName.
有关详细信息,请参阅此link。