Viewholder 中的 Listview 内存不足异常

Listview out of memory exception in viewholder

您好,我正在尝试在 ListView 中显示一系列图像,但是当我 运行 应用程序时,它 运行 内存不足,这与昨晚测试时一样非常奇怪它工作得很好,今天早上回到它时,它知道不再希望打球了。我正在使用 ViewHolder 模式,我想知道是否有人能发现我的错误?

液体适配器

public class LiqAdapter  extends ArrayAdapter<Liq_String> {

List<Liq_String> lstLiq;
LayoutInflater inflater;

public LiqAdapter(Context context, int resource, List<Liq_String> objects) {
    super(context, resource, objects);
    lstLiq = objects;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    ViewHolderLiq viewholder;

    if (view == null){

        viewholder = new ViewHolderLiq();

        view = inflater.inflate(R.layout.custom_winelist, null);
        viewholder.liqImage = (ImageView) view.findViewById(R.id.wineImage);
        viewholder.liqName = (TextView) view.findViewById(R.id.wineName);
        viewholder.liqDes = (TextView) view.findViewById(R.id.wineDes);
        viewholder.liqPrice = (TextView) view.findViewById(R.id.winePrice);

        view.setTag(viewholder);
    }else{
        viewholder = (ViewHolderLiq) view.getTag();
    }

    Liq_String wine = lstLiq.get(position);

    viewholder.setDataIntoViewHolderLiqueurs(wine);

    return view;
}
}

取景器

public class ViewHolderLiq {

public ImageView liqImage;
public TextView liqName;
public TextView  liqDes;
public TextView  liqPrice;

public void setDataIntoViewHolderLiqueurs(Liq_String liqString){
    liqImage.setImageResource(liqString.getIcon_liq());
    liqName.setText(liqString.getLiqName());
    liqDes.setText(liqString.getLiqdes());
    liqPrice.setText(liqString.getLiqPrices());
}
}

错误

FATAL EXCEPTION: main
java.lang.OutOfMemoryError: Failed to allocate a 26620312 byte allocation   with 4194304 free bytes and 11MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.Resources.getDrawable(Resources.java:806)
at android.content.Context.getDrawable(Context.java:458)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at wine.entity.ViewHolderLiq.setDataIntoViewHolderLiqueurs(ViewHolderLiq.java:20)
at wine.Adapter.LiqAdapter.getView(LiqAdapter.java:73)

崩溃: 正如您的堆栈跟踪所说:

"java.lang.OutOfMemoryError: Failed to allocate a 26620312 byte allocation with 4194304 free bytes and 11MB until OOM"

  • 您尝试在您的视图中显示一个巨大的位图。你的phone不喜欢。

两种解法:

  • 减小图片的尺寸,使用较小的图片...以及 重试!

  • 或在您的应用程序的清单中设置 android:largeHeap="true" 标签。它将增加您的堆大小并避免 OutOfMemory 错误,但您的应用程序可能会滞后。

java.lang.OutOfMemoryError: Failed to allocate a 26620312 byte allocation with 4194304 free bytes and 11MB until OOM

OutOfMemoryError

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. OutOfMemoryError objects may be constructed by the virtual machine/Device as if suppression were disabled and/or the stack trace was not writable.

原因

您正在处理大型位图并在 运行 时间加载所有位图。

liqImage.setImageResource(liqString.getIcon_liq()); // Here is problem .Size too large

解决方案

首先,请缩小图片大小。

你的图片太大了,缩小是个好办法:

http://android-coding.blogspot.hu/2011/06/reduce-bitmap-size-using.html