【问题标题】:Listview out of memory exception in viewholder视图中的 Listview 内存不足异常
【发布时间】:2016-09-01 13:38:41
【问题描述】:

您好,我正在尝试在 ListView 中显示一系列图像,但是当我运行应用程序时,它会耗尽内存,这很奇怪,因为昨晚测试它时它运行良好,今天早上返回它时它知道再想打球。我正在使用 ViewHolder 模式,我想知道是否有人能发现我的错误?

LiqAdapter

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)

【问题讨论】:

  • @Alexander 确实如此。

标签: android memory memory-leaks out-of-memory android-viewholder


【解决方案1】:

你的图片太大了,为了缩小它,这是一个很好的解决方案:

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

【讨论】:

    【解决方案2】:

    java.lang.OutOfMemoryError: 未能分配 26620312 字节 分配 4194304 个空闲字节和 11MB 直到 OOM

    OutOfMemoryError

    当 Java 虚拟机无法分配对象时抛出,因为 内存不足,无法再提供内存 垃圾收集器。 OutOfMemoryError 对象可以由 虚拟机/设备好像禁用了抑制和/或 堆栈跟踪不可写。

    原因

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

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

    解决方案

    首先,请缩小图片尺寸。

    【讨论】:

      【解决方案3】:

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

      "java.lang.OutOfMemoryError: 分配 26620312 字节失败 分配 4194304 个空闲字节和 11MB 直到 OOM"

      • 您尝试在视图上显示一个巨大的位图。你的手机不喜欢它。

      两种解决方案:

      • 缩小图像的尺寸,使用更小的图像...和 重试!

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

      【讨论】:

      • @james 很高兴为您提供帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多