GridView Docs 代码不要 运行,为什么?

GridView Docs Code Don't Run, Why?

我正在从官方文档中学习如何使用 GridView here。 所以我尝试了他们添加的 Java 示例代码,但它不起作用。 这里的代码以防上面的代码被更改 link:

布局:

<?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"
        />

OnCreate() 代码:

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
        Toast.makeText(HelloGridView.this, "" + position,
                Toast.LENGTH_SHORT).show();
    }
});

自定义 Java Class(图像适配器):

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}

当我 运行 它时,它立即给我 "Unfortunately ... Stopped" 消息。

来自堆栈跟踪:

ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast 
to android.widget.AbsListView$LayoutParams

如果您不知道 Stack Trace 是什么,请转到 here

我仔细遵循了所有说明,使用了他们建议的示例图像,最重要的是从网站本身复制了所有代码(包括 XML)。

任何人都可以解释。

我认为他们的代码有错误。因此,我将 getView()if 块中的第三块中的一行部分更改为以下行:

imageView.setLayoutParams(new AbsListView.LayoutParams(85, 85));