【问题标题】:Android Out of memory on custom listviewAndroid自定义列表视图内存不足
【发布时间】:2016-08-30 11:56:02
【问题描述】:

您好,我面临的问题是我有一个自定义列表视图,其中包含 7 个位图图像,但是当我尝试滚动时收到内存不足错误,我已更改所有图像大小,但没有我的图像甚至超过 50kb。我曾尝试在清单中更改堆,但我想知道是否有任何建议。我已将呈现给我的错误包括在内。

 Process: mycompany.myapplication, PID: 3275
 java.lang.OutOfMemoryError: Failed to allocate a 12006012 byte allocation with 2033536 free bytes and 1985KB 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) 

我用来填充列表视图的当前适配器

public class myAdapter extends ListFragment {


String[] liqueurs= {"Almond", "Cherry", "Chocolate Orange", "Lemon", "Orange", "Chocolate"};
String[] description = {
        "test",
        "With a delicious bite of fresh cherry, this can be drunk alone, or mixed with the Almond to make a Cherry Bakewell.",
        "Sweet, rich and smooth. Try it over ice-cream, cooked in a cake, or just sipped from a glass.",
        "Your classic Italian drink without the sharp finish. Absolutely delicious after dinner.",
        "Clean, fresh and with a sweet tang. Our orange liqueur is just the thing to wake your taste buds up.",
        "Made from real ground coco beans, surely this counts as 1 of your 5 a day"};
String[] prices = {"£7.99", "£7.99", "£7.99", "£7.99", "£7.99", "£7.99"};
int[] images ={R.drawable.almond, R.drawable.cherry, R.drawable.chocolateorange, R.drawable.lemon, R.drawable.orange, R.drawable.chocolate};


ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;

public LiqueuresAdapter() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment




    HashMap<String, String> map = new HashMap <String, String> ();





    for(int i=0;i<liqueurs.length;i++){

        map = new HashMap<String, String>();
        map.put("liqueurs",liqueurs[i]);
        map.put("description", description[i]);
        map.put("prices", prices[i]);
        map.put("Image", Integer.toString(images[i]));

        data.add(map);


    }

    String[] from = {"liqueurs", "Image", "description", "prices"};



    int[] to={R.id.wineName, R.id.wineImage, R.id.wineDes, R.id.winePrice};

    adapter = new SimpleAdapter(getActivity(), data, R.layout.custom_winelist, from, to);
    setListAdapter(adapter);



    //  return inflater.inflate(R.layout.fragment_contact_us, container, false);

    return super.onCreateView(inflater, container, savedInstanceState);



}


@Override
public void onStart() {
    super.onStart();
}


// Removes duplicate id error by destroying the container and rebuilding it when next selected
@Override
public void onDestroyView() {
    FragmentManager fm = getFragmentManager();

    Fragment xmlFragment = fm.findFragmentById(R.id.fragment_container);
    if (xmlFragment != null) {
        fm.beginTransaction().remove(xmlFragment).commit();
    }

    super.onDestroyView();
}
}

【问题讨论】:

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


    【解决方案1】:

    我推荐你使用 Glide 或 Picasso 来加载、缓存和管理图像,你也可以使用 ViewHolder 设计模式来优化你的 ListView 性能并减少内存负载。基本上 ViewHolder 所做的是重用视图而不增加新视图(这会增加内存)。 这可能会降低你的记忆力。 Here 是我的 GitHub 帐户中有关如何在 ListView 中自定义 ArrayAdapter 以合并 ViewHolder 的示例。 希望对你有帮助!

    【讨论】:

    • 嗨,Isaac,我已经下载了你的项目,它非常有用,看看你如何使用 JSON 来工作很有趣。查看数组适配器类后,是否可以更改您的代码,以便查看我上面提到的数组?
    • 有可能,我周末去看看
    • 没关系,自从你的消息以来,我一直在查找视图持有者,我想我已经解决了问题:D
    猜你喜欢
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多