在android中的GridView,gridView.setAdapater(this)错误

时间:2013-12-24 11:06:45

标签: java android gridview

我遵循一些教程“如何在Android中使用GridView”并尝试制作图库....
但为什么?
我已经和教程一样做了....

此文件GridActivity.java:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

}

和文件ImageAdapter.java:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

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

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

    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    public View getView(int position, View converView, ViewGroup parent) {
        ImageView  imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70,70));
        return imageView;           
    }       

    public Integer[] mThumbIds = {
            R.drawable.image1
    };

}

没有错误...... 但是当我试图用模拟器运行时......

“Unfotunetly,...........已停止”

请....帮助...我..

1 个答案:

答案 0 :(得分:2)

new ImageAdapter(this)需要一个以上下文作为参数的构造函数。

更改此

 public void ImageAdapater(Context c) { //remove void for constructor

 public ImageAdapater(Context c) {

更多信息

http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html