在我的应用程序中,我使用的是 volley imageLoader,但有一个问题我无法理解。当我只调用 response.getBitmap 并将其设置为背景图像视图时它很好,但是当我添加一些代码例如 System.out.println(response.getBitmap) 时它会崩溃。总之,如果我只调用一次 respone.getBitmap 就可以了,但是当两次或更多时它会崩溃。这里有什么问题?
public class NewsAdapter extends BaseAdapter {
private ArrayList<News> mNewsList;
private DefaultActivity mActivity;
private ImageLoader imageLoader;
private RequestQueue mRequestQueue;
public NewsAdapter(DefaultActivity pActivity, ArrayList<News> newsList){
mActivity = pActivity;
mNewsList = newsList;
mRequestQueue = Volley.newRequestQueue(mActivity);
imageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(
BitmapLruCache.getDefaultLruCacheSize()));
}
private class ViewHolder {
public ImageView networkImageView;
public TextView title;
public TextView description;
}
@Override
public int getCount() {
return mNewsList.size();
}
@Override
public News getItem(int i) {
return mNewsList.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder;
final News news = getItem(i);
if (view == null){
final LayoutInflater li = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.orange_news_item, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.networkImageView = (ImageView)view.findViewById(R.id.imageView);
viewHolder.title = (TextView)view.findViewById(R.id.title);
viewHolder.description = (TextView)view.findViewById(R.id.description);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.title.setText(Html.fromHtml(news.getTitle()));
viewHolder.description.setText(Html.fromHtml(news.getBody()));
imageLoader.get(news.getImageThumbUrl(),new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
viewHolder.networkImageView.setImageBitmap(response.getBitmap());
System.out.println("bitmap="+response.getBitmap());
System.out.println("width="+response.getBitmap().getWidth());
System.out.println("height="+response.getBitmap().getHeight());
}
@Override
public void onErrorResponse(VolleyError error) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
return view;
}
}