当前位置: 代码迷 >> Android >> android.content.res.Resources $ NotFoundException:资源ID#0x0可绘制
  详细解决方案

android.content.res.Resources $ NotFoundException:资源ID#0x0可绘制

热度:30   发布时间:2023-08-04 11:58:33.0

我收到此运行时错误

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

}

我需要更改它,但同时我还需要将其更改为将我的图片更改为适当的图片的方式,因为我正在使用它为不同的组拖动不同的图片。 谁能告诉我该怎么做?

更换

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.

有关更多详细信息,请参考此 。

  相关解决方案