Android中GridView用GridAdapter展示图文崩溃,报InflateException及空指针异常
Hey there, let's break down why your Android app is crashing and fix it step by step:
1. Root Cause of InflateException
The main crash trigger is a tiny typo in your grid_item.xml layout. Look at this line:
<view android:layout_width="match_parent" android:layout_height="1.5dp" android:background="#e6e6e6"/>
Android layout tags are case-sensitive! <view> (lowercase 'v') is not a valid Android component—you need to use <View> (uppercase 'V') instead. The LayoutInflater can't recognize the lowercase tag, which throws the InflateException.
2. Fixing the Layout File
Update that line in grid_item.xml to:
<View android:layout_width="match_parent" android:layout_height="1.5dp" android:background="#e6e6e6"/>
3. Fixing NullPointerException & Optimizing Adapter
Your GridAdapter has a critical issue with view recycling: when convertView is reused (the else block), you're not updating the text and image content. This would cause wrong data to show up as you scroll, and could lead to NullPointerException if the views aren't properly referenced later.
Let's refactor the adapter using the ViewHolder pattern—this fixes the recycling issue and boosts performance too:
Updated GridAdapter.java
public class GridAdapter extends BaseAdapter { private Context mContext; private final String[] web; private final int[] Imageid; private LayoutInflater inflater; public GridAdapter(Context c,String[] web,int[] Imageid ) { mContext = c; this.Imageid = Imageid; this.web = web; // Initialize LayoutInflater once in constructor (better performance) inflater = LayoutInflater.from(mContext); } @Override public int getCount() { return web.length; } @Override public Object getItem(int position) { return web[position]; // Return actual item instead of null (useful for click listeners) } @Override public long getItemId(int position) { return position; // Return position as stable ID } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // Inflate layout and attach to parent for proper layout params convertView = inflater.inflate(R.layout.grid_item, parent, false); holder = new ViewHolder(); holder.textView = convertView.findViewById(R.id.product_name); holder.imageView = convertView.findViewById(R.id.pic); // Store holder in convertView's tag convertView.setTag(holder); } else { // Retrieve existing holder from tag holder = (ViewHolder) convertView.getTag(); } // Update data for current position (works for both new and recycled views) holder.textView.setText(web[position]); holder.imageView.setImageResource(Imageid[position]); return convertView; } // ViewHolder to cache views and avoid repeated findViewById calls private static class ViewHolder { TextView textView; ImageView imageView; } }
4. Bonus: Minor Improvement in GridActivity
In your item click listener, there's a small redundant + sign:
Toast.makeText(GridActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
Change it to:
Toast.makeText(GridActivity.this, "You Clicked at " + web[position], Toast.LENGTH_SHORT).show();
It doesn't cause crashes, but it's cleaner.
Final Checks
- Make sure
R.drawable.product_imagexists in your drawable folders—missing resources can also cause crashes. - Verify that all IDs in your layouts match the ones you're referencing in code (e.g.,
product_name,pic).
After applying these fixes, your GridView should load without crashes and display content correctly as you scroll.
内容的提问来源于stack exchange,提问作者johnson

