我已经写了一个适配器为GridView显示的照片是从网上和部分代码下载被列为遵循Android的IndexOutOfBound异常
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(位置)”
现在我已经写了一个意向来打开另一个活动在代码的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: Unable to start activity
ComponentInfo{com.android.mcsis/com.android.mcsis.SitePhotoFullScreen}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
我猜的ArrayList imageAdapter.drawablesFromUrl.get(position);
是空的。任何我错了?
能否详细说明如何使用Application类对象?我不知道如何使用,谢谢! – user1411475 2012-07-11 08:02:41
请看下面的链接.http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/ – Shachillies 2012-07-11 09:26:51