我已经为 gridView 编写了一个适配器来显示从 Internet 下载的照片,部分代码如下所示
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ArrayList<Drawable> drawablesFromUrl = new ArrayList<Drawable>();
public ImageAdapter(Context c) {
mContext = c;
}
public void addItem(Drawable item) {
drawablesFromUrl.add(item);
}
public int getCount() {
return drawablesFromUrl.size();
}
public Drawable getItem(int position) {
return drawablesFromUrl.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageDrawable(drawablesFromUrl.get(position));
return imageView;
}//View getView
}//class ImageAdapter
它工作正常,下载的照片存储在 ArrayList "drawablesFromUrl.get(position)"
现在我已经写了一个意图来打开另一个活动来在 imagView 中显示一张单独的照片,部分代码列表如下:
SitePhotoGallery pg = new SitePhotoGallery();
ImageAdapter imageAdapter = pg.new ImageAdapter(this);
Drawable dPhoto=imageAdapter.drawablesFromUrl.get(position); //(ERROR in this line)
BitmapDrawable bPhoto = (BitmapDrawable) dPhoto;
Bitmap snoop = bPhoto.getBitmap();
//Bitmap snoop = BitmapFactory.decodeResource(getResources(),imageAdapter.mThumbIds[position]);
img.setImageBitmap(snoop);
img.setMaxZoom(4f);
setContentView(img);
它提示错误:
07-11 15:22:11.458: E/AndroidRuntime(12856): java.lang.RuntimeException: 无法启动活动
ComponentInfo{com.android.mcsis/com.android.mcsis.SitePhotoFullScreen}: java.lang.IndexOutOfBoundsException: 无效索引 0,大小为 0
我猜arraylistimageAdapter.drawablesFromUrl.get(position);
是空的。有什么我做错了吗?