【发布时间】:2014-09-16 00:20:29
【问题描述】:
我无法将位图数据从片段类发送到自定义适配器类。日志显示以下错误。
FATAL EXCEPTION: main
Process: com.example.readersden, PID: 21025
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
atjava.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.readersden.CardAdapter.getView(CardAdapter.java:82)
Fragment 类(AsyncTask 的 onPostExecute 方法内部):
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
try {
JSONObject imageInfo = volumeObject.getJSONObject("imageLinks");
new GetBookThumb().execute(imageInfo.getString("thumbnail"));
} catch (JSONException je) {
bookThumb.add(0, bmp);
je.printStackTrace();
}
Fetch Image 类(在 AsyncTask 类的 doInBackground 方法中):
InputStream thumbIn = thumbConn.getInputStream();
BufferedInputStream thumbBuff = new BufferedInputStream(thumbIn);
thumbImg = BitmapFactory.decodeStream(thumbBuff);
bookThumb.add(0, thumbImg);
thumbBuff.close();
thumbIn.close();
自定义适配器类(内部 getView 方法):
ArrayList<Bitmap> bookThumb;
e.thumb.setImageBitmap(bookThumb.get(pos));
我是 android 开发的新手,它非常有趣! ^.^ 谢谢!
编辑:
public CardAdapter(ArrayList<String> bookTitle,
ArrayList<String> bookAuthor, ArrayList<String> bookPublishDate,
ArrayList<String> bookPages, ArrayList<Bitmap> bookThumb,
Activity mAct) {
super();
this.bookTitle = bookTitle;
this.bookAuthor = bookAuthor;
this.bookPublishDate = bookPublishDate;
this.bookPages = bookPages;
this.bookThumb = bookThumb;
this.mAct = mAct;
mInflater = (LayoutInflater) mAct
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int pos, View view, ViewGroup viewGroup) {
LayoutField e;
if (view == null) {
view = mInflater.inflate(R.layout.layout_card, viewGroup, false);
e = new LayoutField();
e.title = (TextView) view.findViewById(R.id.book_title);
e.thumb = (ImageView) view.findViewById(R.id.book_thumbnail);
e.author = (TextView) view.findViewById(R.id.book_author);
e.publishDate = (TextView) view
.findViewById(R.id.book_publish_date);
e.pages = (TextView) view.findViewById(R.id.book_pages);
view.setTag(e);
} else
e = (LayoutField) view.getTag();
e.title.setText(bookTitle.get(pos));
e.thumb.setImageBitmap(bookThumb.get(pos));
e.author.setText(bookAuthor.get(pos));
e.publishDate.setText(bookPublishDate.get(pos));
e.pages.setText(bookPages.get(pos));
return view;
}
class LayoutField {
TextView title, author, publishDate, pages;
ImageView thumb;
}
}
传递文本工作得很好!只是图像给出了超出范围的索引。
【问题讨论】:
-
需要更多代码。您如何将 arrayList 分配给 bookThumb ?
-
*.get(pos)数组之一没有任何元素。 -
嗨!我正在使用 bookThumb.add(index, bitmap); 将位图图像分配给 arraylist
;方法。 -
@nija 它的 arraylist
将索引抛出边界错误。可能它的 null 或数据存储在多个索引中?我需要检查任何例外情况吗?如果是这样..在哪里? -
@rogerthatcode
bookThumb没有元素,因为异常显示size is 0。那就是问题所在。将日志记录添加到异常所在的位置以及添加到数组的位置以查看操作顺序?
标签: java android android-fragments arraylist bitmap