官方文档中的GridView示例代码无法运行,原因何在?
Hey there, let’s dig into why your GridView example isn’t running. First, there’s an obvious issue with the partial layout code you shared, plus some common GridView setup pitfalls we can walk through together.
First: Fix Your Incomplete Layout XML
Your provided layout cuts off mid-attribute, which will definitely trigger an XML parsing error. Here’s a complete, corrected version of a standard GridView layout matching the official example structure:
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:columnWidth="90dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" />
Make sure all attributes are fully written and closed—truncated values like your unfinished android:verticalSpacing will crash your app before it even loads.
Common Java Code Issues (And Fixes)
If your layout is now complete but the app still fails, check these frequent mistakes in your Java code:
Forgot to set the Adapter: GridView relies on an Adapter to populate data. Here’s a simple working ArrayAdapter example paired with the layout above:
import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.ArrayAdapter; public class GridViewDemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_grid_layout_filename); GridView gridView = findViewById(R.id.gridview); // Sample data to populate the grid String[] gridItems = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8"}; // Create and attach the adapter to the GridView ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, gridItems); gridView.setAdapter(adapter); } }Double-check that you’ve called
gridView.setAdapter(adapter)—this is an incredibly easy step to overlook.Custom Adapter Errors: If you’re using a custom Adapter (like the official example might demonstrate), ensure your
getView()method is properly implemented. Common missteps here include:- Not reusing
convertView(which can cause crashes or performance issues) - Incorrectly inflating the grid item layout
- Binding data to the wrong views in the item layout
- Not reusing
Activity Registration: Confirm your GridView activity is registered in
AndroidManifest.xml—this oversight will cause a crash when trying to launch the activity.
Final Quick Checks
- Make sure your layout filename matches what you reference in
setContentView()(e.g., if your layout isactivity_grid.xml, useR.layout.activity_grid). - Check Logcat for specific error messages—they’ll point directly to the problem (like XML parsing failures, null pointers from missing views, etc.).
Once you address these common issues, your GridView should run smoothly!
内容的提问来源于stack exchange,提问作者Tayan

