简体   繁体   中英

setImageResource() Returns null

I am trying to get images from my asset folder and setting them to my variable called insectImage. Here is what I've done:

InsectRecyclerAdapter.InsectHolder insectHolder = holder;
Insect insectItem = (Insect) recyclerViewItems.get(position);

String imageName = insectItem.getImageName();
Log.d("imageName",imageName);
int imageResID = mContext.getResources().getIdentifier(imageName, "drawable", mContext.getPackageName());
Log.d("imageID", ""+imageResID);
insectHolder.insectImage.setImageResource(imageResID);

imageResID returns 0 in the logcat, but the imageName variable is set to one of my images that exist in my assets folder. Logcat is saying that setImageResource(imageResID) is returning null.

How can I set the imageResID to the imageName that's in my asset folder?

If imageResId is zero, that means that getIdentifier cannot find a resource with the name information that you gave.

Reference:

That's the root cause of the problem. What happens after that is just "consequences" of the earlier error.

Solution: find out why the resource identifier lookup fails.

  • Have you got the name wrong?
  • The defType ?
  • The defPackage ?
  • Is the image resource there?
  • Is it in the right place?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM