Volley Plus OutOfMemoryError in List Adapter

不羁的心 提交于 2019-12-02 21:24:26

问题


I am using Volley Plus library. I am showing ListView with image and text view with no error, but when i scroll up and down lots of time in this listview it get OutOfMemoryError and crash the application.

Here is LogCat :

java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
        at java.lang.Thread.nativeCreate(Native Method)
        at java.lang.Thread.start(Thread.java:1063)
        at com.android.volley.RequestQueue.start(RequestQueue.java:135)
        at com.android.volley.cache.plus.SimpleImageLoader.newRequestQueue(SimpleImageLoader.java:353)
        at com.android.volley.cache.plus.SimpleImageLoader.<init>(SimpleImageLoader.java:130)
        at com.symnepal.adapter.ProductListAdapter.getView(ProductListAdapter.java:73)
        at android.widget.AbsListView.obtainView(AbsListView.java:2344)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillGap(ListView.java:662)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4968)
        at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3398)
        at android.widget.AbsListView.onTouchMove(AbsListView.java:3774)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:3612)
        at android.view.View.dispatchTouchEvent(View.java:8388)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
        at android.view.View.dispatchPointerEvent(View.java:8578)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
        at android.view.InputEventReceiver.

Here is my Adapter class :

public class ProductListAdapter extends BaseAdapter {

private Context mContext;
private List<Products> dataArray;

public ProductListAdapter(Context context, List<Products> d) {
    mContext = context;
    dataArray = d;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_row_item_products, null);

        ViewHolder viewHolder = new ViewHolder();
        viewHolder.title = (TextView) rowView.findViewById(R.id.tvName);
        viewHolder.image = (ImageView) rowView.findViewById(R.id.list_image);

        rowView.setTag(viewHolder);
    }

    Products list;
    list = dataArray.get(position);

    ViewHolder holder = (ViewHolder) rowView.getTag();
    holder.title.setText(list.getName());

    DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(mContext, "CacheDirectory");
    cacheParams.setMemCacheSizePercent(0.5f);
    SimpleImageLoader mImageFetcher = new SimpleImageLoader(mContext, null, cacheParams);
    mImageFetcher.get(list.getThumb(), holder.image);

    return rowView;
}

static class ViewHolder {
    TextView title;
    ImageView image;
}
}

Please help me to get out from this problem.


回答1:


The problem is that you keep loading images but the memory they fill isn't released afterwards. You could simply if you are recycling a row test if the image is a bitmap and then call recycle() on it. You might use something like this:

if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_row_item_products, null);

        ViewHolder viewHolder = new ViewHolder();
        viewHolder.title = (TextView) rowView.findViewById(R.id.tvName);
        viewHolder.image = (ImageView) rowView.findViewById(R.id.list_image);

        rowView.setTag(viewHolder);
}
else {
    ViewHolder holder = (ViewHolder) rowView.getTag();
    if(holder.image.getDrawable() instanceof BitmapDrawable)
        ((BitmapDrawable) holder.image.getDrawable).getBitmap().recycle();
}



回答2:


You should create SimpleImageLoader in onCreate() of Activity or some where else

    DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(mContext, "CacheDirectory");
    cacheParams.setMemCacheSizePercent(0.5f);
    SimpleImageLoader mImageFetcher = new SimpleImageLoader(mContext, null, cacheParams);

If you are still facing OOM exception then you can use NetworkImageView.

Detailed example is present in github sample




回答3:


Here is the solution for this Problem with changes in Adapter class. In which i placed

    DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(mContext, "CacheDirectory");
    cacheParams.setMemCacheSizePercent(0.5f);
    SimpleImageLoader mImageFetcher = new SimpleImageLoader(mContext, null, cacheParams);

Inside if (rowView == null) which prevents to use above code repetitively in Adabpter Class.



来源:https://stackoverflow.com/questions/27211417/volley-plus-outofmemoryerror-in-list-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!