Android项目RecyclerViewAdapter无法加载图片问题求助
Hey there! Let's figure out why your images aren't loading in the CardView. The Fragment's getContext() isn't the problem here—it's fully capable of accessing app resources. Let's walk through the most common fixes step by step:
1. Verify Image Resource Names Match Exactly
Android resource filenames are case-sensitive, so even a tiny mismatch (like Image1 vs image1) will cause getIdentifier to return 0.
- Make sure the string returned by
newsList.get(position).getImageName()matches the filename in yourdrawablefolder without the file extension (e.g., usecyber_news_1instead ofcyber_news_1.png). - Check for extra spaces or hidden characters in the string read from your
news_items.txtfile. Add.trim()when parsing to clean up whitespace:image_name = line.substring(colonIndex+1).trim();
2. Validate the getIdentifier Call
Double-check your resource lookup code for typos. Your current code looks correct, but confirm these details:
// Correct syntax for resource lookup int resID = mContext.getResources().getIdentifier( newsList.get(position).getImageName(), "drawable", mContext.getPackageName() );
- If
resIDlogs as0(check yourMember100log), the resource isn't found—go back to verifying the image name. - If
resIDis non-zero but the image still doesn't show, check your ImageView's layout: ensure it has a defined width/height (notwrap_contentif the image has no intrinsic size) and isn't blocked by other views.
3. Replace getIdentifier with a More Reliable Approach
getIdentifier is slow and error-prone. A better option is to store resource IDs directly in your News class:
- Update the
Newsclass to use anintfor the image resource instead of a string:public class News { private int imageResId; // ... other fields public News(String category, int imageResId, String newsTitle, String newsUrl, Date newsDate) { this.imageResId = imageResId; // ... initialize other fields } public int getImageResId() { return imageResId; } } - When creating
Newsobjects in your Fragment, pass the resource ID directly (e.g.,R.drawable.cyber_news_1):cybersecurity_list.add(new News(n.getCategory(), R.drawable.your_image_name, n.getNews_title(), n.getNews_url(), n.getNews_date())); - In your Adapter, set the image directly without lookup:
holder.imageview.setImageResource(newsList.get(position).getImageResId());
4. Strengthen Context Usage in Fragment
While getContext() works, you can use requireContext() (which throws an exception if context is null, helping catch issues early) when initializing the Adapter:
customAdapter adapter = new customAdapter(requireContext(), cybersecurity_list);
Final Quick Checks
- Ensure your images are placed in the base
drawablefolder (not version-specific folders likedrawable-v24if you need them to work on all devices). - Test with a hardcoded resource ID first to confirm the ImageView works:
holder.imageview.setImageResource(R.drawable.test_image);
If you try these steps and still have issues, share the exact value of image_name from your logs and the filename of one of your drawable images—we can dig deeper!
内容的提问来源于stack exchange,提问作者a random programmer

