【问题标题】:android.content.res.Resources$NotFoundException: Resource ID #0x0 drawableandroid.content.res.Resources$NotFoundException:资源 ID #0x0 可绘制
【发布时间】:2015-10-26 00:19:05
【问题描述】:

我收到此运行时错误

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

}

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

【问题讨论】:

    标签: java android compiler-errors


    【解决方案1】:

    替换

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多