我已按照有关 GridView 的教程进行操作。我正在尝试通过适配器将 ImageView 与布局 xml 文件中的 GridView 绑定。我从 mainActivity 捕获了 ImageView,并通过 ImageAdapter 的构造函数输入 ImageView 或将 ImageView 设为静态来尝试两种方法。它们都返回运行时异常。
//capturing imageView in the mainActivity
public static ImageView IMAGE_VIEW;
IMAGE_VIEW=(ImageView) findViewById(R.id.imageView1);
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
// It should return 16 ImageViews
return 16;
}
.
.
.
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 GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(20, 20, 4, 4);
} else {
imageView = (ImageView) convertView;
}
imageView=MainActivity.IMAGE_VIEW; //I suppose here is the problem
// the code underneath works fine for an Image File not for the ImageView
// imageView.setImageResource(R.drawable.crazy);
return imageView;
}
}
这里有什么错误?解决方案是什么?