GridView文档代码不运行,为什么?

时间:2018-04-26 22:52:55

标签: android android-gridview

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

布局:

<?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类(图像适配器):

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
    };
}

当我跑步时,它立即给我“不幸......停止”的消息。

来自堆栈跟踪:

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

如果您不知道堆栈跟踪的内容here

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

任何人都可以解释。

1 个答案:

答案 0 :(得分:0)

我认为他们的代码中存在错误。所以我部分更改了if getView()块中第三个块中的一行,如下所示:

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