使用项目滚动列表视图时出现此错误。
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
有人可以帮忙吗?
编辑
public class ItemsListAdapter2 extends BaseAdapter {
private final float NUMBER_OF_ITEMS_PER_GRID = 2;
private final int AD_POSITION = 4;
private Context context;
private ArrayList<ArrayList<ItemResponse>> allData = new ArrayList<ArrayList<ItemResponse>>();
public class ViewHolder {
// for Item1
LinearLayout itemLayout1;
ImageView itemImage1;
TextView itemDate1;
ImageView itemTagImage1;
TextView normalItemPrice1;
TextView pinnedItemPrice1;
TextView itemTitle1;
TextView itemDescription1;
// for Item2
LinearLayout itemLayout2;
ImageView itemImage2;
TextView itemDate2;
ImageView itemTagImage2;
TextView normalItemPrice2;
TextView pinnedItemPrice2;
TextView itemTitle2;
TextView itemDescription2;
RelativeLayout adLayout;
ImageView adUnit;
}
public ItemsListAdapter2(Context context, ArrayList<ItemResponse> data) {
this.context = context;
setData(data);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_listings2, null);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.itemLayout1 = (LinearLayout) convertView.findViewById(R.id.item1_layout);
holder.itemImage1 = (ImageView) convertView.findViewById(R.id.item_image1);
holder.itemDate1 = (TextView) convertView.findViewById(R.id.item_date1);
holder.itemTagImage1 = (ImageView) convertView.findViewById(R.id.tag_image1);
holder.normalItemPrice1 = (TextView) convertView.findViewById(R.id.normal_item_price1);
holder.pinnedItemPrice1 = (TextView) convertView.findViewById(R.id.pinned_item_price1);
holder.itemTitle1 = (TextView) convertView.findViewById(R.id.item_title1);
holder.itemDescription1 = (TextView) convertView.findViewById(R.id.item_description1);
holder.itemLayout2 = (LinearLayout) convertView.findViewById(R.id.item2_layout);
holder.itemImage2 = (ImageView) convertView.findViewById(R.id.item_image2);
holder.itemDate2 = (TextView) convertView.findViewById(R.id.item_date2);
holder.itemTagImage2 = (ImageView) convertView.findViewById(R.id.tag_image2);
holder.normalItemPrice2 = (TextView) convertView.findViewById(R.id.normal_item_price2);
holder.pinnedItemPrice2 = (TextView) convertView.findViewById(R.id.pinned_item_price2);
holder.itemTitle2 = (TextView) convertView.findViewById(R.id.item_title2);
holder.itemDescription2 = (TextView) convertView.findViewById(R.id.item_description2);
holder.adLayout = (RelativeLayout) convertView.findViewById(R.id.banner_ad);
holder.adUnit = (ImageView) convertView.findViewById(R.id.ad_view_image);
if (allData.get(position) != null && allData.get(position).size() > 0) {
setItem1Data(holder, position);
setItem2Data(holder, position);
}
if ((position + 1) % AD_POSITION == 0) {
// show ad
holder.adLayout .setVisibility(View.VISIBLE);
//initializeBannerAd(convertView, holder.adUnit);
} else {
holder.adLayout .setVisibility(View.GONE);
}
return convertView;
}
@Override
public int getCount() {
return allData.size();
}
@Override
public Object getItem(int position) {
try {
return allData.get(position);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
return null;
}
}
@Override
public long getItemId(int position) {
try {
return allData.indexOf(getItem(position));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
return -1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
private void initData (ArrayList<ItemResponse> data) {
int numberOfItems = (int) Math.ceil(data.size() / NUMBER_OF_ITEMS_PER_GRID);
ArrayList<ArrayList<ItemResponse>> tempData = new ArrayList<ArrayList<ItemResponse>>(numberOfItems);
for (int i = 0; i < numberOfItems; i++) {
ArrayList<ItemResponse> items = new ArrayList<ItemResponse>();
for (int j = 0; j < NUMBER_OF_ITEMS_PER_GRID; j++) {
try {
items.add(data.get(j));
} catch (Exception e) {
e.printStackTrace();
}
}
tempData.add(i, items);
// clear
data.removeAll(items);
}
for (int n = 0; n < tempData.size(); n++) {
allData.add(tempData.get(n));
}
}
public void setData (ArrayList<ItemResponse> data) {
allData.clear();
ArrayList<ItemResponse> mData = new ArrayList<ItemResponse>();
for (int i = 0; i < data.size(); i++) {
mData.add(data.get(i));
}
initData(mData);
}
public void setItem1Data (ViewHolder holder , int position) {
try {
holder.itemLayout1.setVisibility(View.VISIBLE);
final ItemResponse current = allData.get(position).get(0);
if (current != null) {
if (current.getId() == ForSaleConstants.POST_AD_LISTING_ID) {
String postAdPlaceholder = "";
ForSaleApplication app = (ForSaleApplication) context.getApplicationContext();
if (app.getDefaultLocale().toLowerCase().startsWith(ForSaleConstants.ARABIC_LANGUAGE)) {
postAdPlaceholder = PhoneUtils.getStringFromSharedPreference(context, ForSaleConstants.PINNING_IMAGE_AR_URL_KEY);
} else {
postAdPlaceholder = PhoneUtils.getStringFromSharedPreference(context, ForSaleConstants.PINNING_IMAGE_EN_URL_KEY);
}
if (PhoneUtils.isEmpty(postAdPlaceholder) == false) {
PhoneUtils.setImage(context, holder.itemImage1, postAdPlaceholder);
}
holder.itemDate1.setVisibility(View.GONE);
holder.itemTagImage1.setVisibility(View.GONE);
holder.normalItemPrice1.setVisibility(View.GONE);
holder.pinnedItemPrice1.setVisibility(View.GONE);
holder.itemTitle1.setVisibility(View.GONE);
holder.itemDescription1.setVisibility(View.GONE);
// TODO handle on click listener for placeholder.
} else {
holder.itemDate1.setVisibility(View.VISIBLE);
holder.itemTagImage1.setVisibility(View.VISIBLE);
holder.itemTitle1.setVisibility(View.VISIBLE);
holder.itemDescription1.setVisibility(View.VISIBLE);
if (current.getImages() != null && current.getImages().size() > 0) {
PhoneUtils.setImage(context, holder.itemImage1, current.getImages().get(0));
}
holder.itemDate1.setText(current.getDatePublished());
if (current.isPinned() == true) {
holder.itemTagImage1.setImageResource(R.drawable.tag_pinned);
}
if (current.isPinned() == true) {
if (ForSaleDataManager.getInstance().getCurrentRegion() != null)
holder.pinnedItemPrice1.setText(current.getAskingPrice() + " " + ForSaleDataManager.getInstance().getCurrentRegion().getRegionCode());
else
holder.pinnedItemPrice1.setText("" + current.getAskingPrice());
holder.normalItemPrice1.setVisibility(View.GONE);
holder.pinnedItemPrice1.setVisibility(View.VISIBLE);
holder.itemTagImage1.setVisibility(View.VISIBLE);
} else {
if (ForSaleDataManager.getInstance().getCurrentRegion() != null)
holder.normalItemPrice1.setText(current.getAskingPrice() + " " + ForSaleDataManager.getInstance().getCurrentRegion().getRegionCode());
else
holder.normalItemPrice1.setText("" + current.getAskingPrice());
holder.normalItemPrice1.setVisibility(View.VISIBLE);
holder.pinnedItemPrice1.setVisibility(View.GONE);
holder.itemTagImage1.setVisibility(View.GONE);
}
holder.itemTitle1.setText(current.getTitle());
holder.itemDescription1.setText(current.getDescription());
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ItemDetailsActivity.class);
intent.putExtra(ForSaleConstants.ACTIVITY_ITEM_OBJECT, current);
context.startActivity(intent);
}
};
holder.itemImage1.setOnClickListener(clickListener);
holder.itemTitle1.setOnClickListener(clickListener);
holder.itemDescription1.setOnClickListener(clickListener);
}
}
} catch (Exception e) {
e.printStackTrace();
holder.itemLayout1.setVisibility(View.INVISIBLE);
}
}
public void setItem2Data (ViewHolder holder , int position) {
try {
holder.itemLayout2.setVisibility(View.VISIBLE);
final ItemResponse current = allData.get(position).get(1);
if (current != null) {
if (current.getId() == ForSaleConstants.POST_AD_LISTING_ID) {
String postAdPlaceholder = "";
ForSaleApplication app = (ForSaleApplication) context.getApplicationContext();
if (app.getDefaultLocale().toLowerCase().startsWith(ForSaleConstants.ARABIC_LANGUAGE)) {
postAdPlaceholder = PhoneUtils.getStringFromSharedPreference(context, ForSaleConstants.PINNING_IMAGE_AR_URL_KEY);
} else {
postAdPlaceholder = PhoneUtils.getStringFromSharedPreference(context, ForSaleConstants.PINNING_IMAGE_EN_URL_KEY);
}
if (PhoneUtils.isEmpty(postAdPlaceholder) == false) {
PhoneUtils.setImage(context, holder.itemImage2, postAdPlaceholder);
} else {
PhoneUtils.setImage(context, holder.itemImage2, null, R.drawable.placeholder_squre);
}
holder.itemDate2.setVisibility(View.GONE);
holder.itemTagImage2.setVisibility(View.GONE);
holder.normalItemPrice2.setVisibility(View.GONE);
holder.pinnedItemPrice2.setVisibility(View.GONE);
holder.itemTitle2.setVisibility(View.GONE);
holder.itemDescription2.setVisibility(View.GONE);
// TODO handle on click listener for placeholder.
} else {
holder.itemDate2.setVisibility(View.VISIBLE);
holder.itemTagImage2.setVisibility(View.VISIBLE);
holder.itemTitle2.setVisibility(View.VISIBLE);
holder.itemDescription2.setVisibility(View.VISIBLE);
if (current.getImages() != null && current.getImages().size() > 0) {
PhoneUtils.setImage(context, holder.itemImage2, current.getImages().get(0));
}
holder.itemDate2.setText(current.getDatePublished());
if (current.isPinned() == true) {
holder.itemTagImage2.setImageResource(R.drawable.tag_pinned);
} else {
holder.itemTagImage2.setImageResource(R.drawable.tag);
}
if (current.isPinned() == true) {
if (ForSaleDataManager.getInstance().getCurrentRegion() != null)
holder.pinnedItemPrice2.setText(current.getAskingPrice() + " " + ForSaleDataManager.getInstance().getCurrentRegion().getRegionCode());
else
holder.pinnedItemPrice2.setText("" + current.getAskingPrice());
holder.normalItemPrice2.setVisibility(View.GONE);
holder.pinnedItemPrice2.setVisibility(View.VISIBLE);
holder.itemTagImage2.setVisibility(View.VISIBLE);
} else {
if (ForSaleDataManager.getInstance().getCurrentRegion() != null)
holder.normalItemPrice2.setText(current.getAskingPrice() + " " + ForSaleDataManager.getInstance().getCurrentRegion().getRegionCode());
else
holder.normalItemPrice2.setText("" + current.getAskingPrice());
holder.normalItemPrice2.setVisibility(View.VISIBLE);
holder.pinnedItemPrice2.setVisibility(View.GONE);
holder.itemTagImage2.setVisibility(View.GONE);
}
holder.itemTitle2.setText(current.getTitle());
holder.itemDescription2.setText(current.getDescription());
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ItemDetailsActivity.class);
intent.putExtra(ForSaleConstants.ACTIVITY_ITEM_OBJECT, current);
context.startActivity(intent);
}
};
holder.itemImage2.setOnClickListener(clickListener);
holder.itemTitle2.setOnClickListener(clickListener);
holder.itemDescription2.setOnClickListener(clickListener);
}
}
} catch (Exception e) {
e.printStackTrace();
holder.itemLayout2.setVisibility(View.INVISIBLE);
}
}
}
并且还找到了加载方法,我正在实现滚动加载更多项目
private void loadData (boolean inBackground) {
int categoryId = -1;
if (mParentCategory != null)
categoryId = mParentCategory.getId();
mIsInBackground = inBackground;
if (mIsInBackground == false)
showProgressDialog();
else
disableBackButton();
// TODO districts.
ForSaleServerManager.getInstance().getListings(this, mSearchQuery,
categoryId, -1, mSellingTypeId, mPageNumber, this);
}
以下是在适配器中设置数据
if (mPageNumber == 1) {
// add all pinning items.
if (items.getPinnedItems() != null && items.getPinnedItems().size() > 0)
allItems.addAll(items.getPinnedItems());
if (mMode == MODE_SEARCH) {
// handle post ad listing item.
if ((allItems.size() % 2) != 0)
allItems.add(new ItemResponse(ForSaleConstants.POST_AD_LISTING_ID));
}
}
// add all normal items.
if (items.getNormalItems() != null && items.getNormalItems().size() > 0)
for (int i = 0; i < items.getNormalItems().size(); i++) {
if (allItems.contains(items.getNormalItems().get(i)) == false)
allItems.add(items.getNormalItems().get(i));
}
if (allItems != null && allItems.size() > 0) {
if (mAdapter == null) {
mAdapter = new ItemsListAdapter2(ItemsActivity.this, allItems);
mListView.setAdapter(mAdapter);
mListView.setVisibility(View.VISIBLE);
mNoDataFound.setVisibility(View.GONE);
} else {
mAdapter.setData(allItems);
mAdapter.notifyDataSetChanged();
mListView.setVisibility(View.VISIBLE);
mNoDataFound.setVisibility(View.GONE);
}
} else {
mListView.setVisibility(View.GONE);
mNoDataFound.setVisibility(View.VISIBLE);
showNoData(R.drawable.icon_listing_unselected, R.string.error_categories_found);
}