Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在自定义listview上内存不足_Android_Listview_Memory_Memory Leaks_Out Of Memory - Fatal编程技术网

Android在自定义listview上内存不足

Android在自定义listview上内存不足,android,listview,memory,memory-leaks,out-of-memory,Android,Listview,Memory,Memory Leaks,Out Of Memory,您好,我面临的问题是,我有一个自定义列表视图,其中包含7个位图图像,但当我尝试滚动时,收到内存不足错误,我更改了所有图像的大小,没有一个图像超过50kb。我尝试过在清单内部更改堆,但我想知道是否有任何建议。我已经包括了提交给我的错误 Process: mycompany.myapplication, PID: 3275 java.lang.OutOfMemoryError: Failed to allocate a 12006012 byte allocation with 2033536

您好,我面临的问题是,我有一个自定义列表视图,其中包含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) 
用于填充listview的当前适配器

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();
}
}
公共类myAdapter扩展了ListFragment{ 字符串[]利口酒={“杏仁”、“樱桃”、“巧克力橙”、“柠檬”、“橙”、“巧克力”}; 字符串[]说明={ “测试”, “加上一口美味的新鲜樱桃,可以单独饮用,也可以与杏仁混合制成樱桃烤盘。”, “甜、浓、滑。可以在冰淇淋上品尝,也可以在蛋糕上烹调,或者只是从杯子里啜饮。”, “你的经典意大利饮料,没有浓烈的余味。饭后绝对美味。”, “干净、新鲜、有甜味。我们的橙色利口酒正是唤醒你味蕾的好东西。”, “由真正的可可豆磨碎而成,这肯定是你每天5粒可可豆中的1粒”}; 字符串[]价格={“7.99英镑”、“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数据=新的ArrayList(); SimpleAdapter适配器; 公共液化气调节剂(){ //必需的空公共构造函数 } @凌驾 创建视图上的公共视图(布局、充气机、视图组容器、, Bundle savedInstanceState){ //为该碎片膨胀布局 HashMap=newhashmap();
对于(int i=0;i我建议您使用Glide或Picasso来加载、缓存和管理图像,并且您还可以使用ViewHolder设计模式来优化ListView性能并减少内存中的负载。基本上,ViewHolder所做的是重用视图,而不增加新视图(这会增加内存)。 这可能会降低你的记忆力。 是我的GitHub帐户中的一个示例,介绍了如何在ListView中自定义ArrayAdapter以合并ViewHolder。
我希望这会有所帮助!

我建议您使用Glide或毕加索来加载、缓存和管理图像,并且您还可以使用ViewHolder设计模式来优化ListView性能并减少内存中的负载。基本上,ViewHolder所做的是在不增加新视图的情况下重用视图(这会增加内存)。 这可能会降低你的记忆力。 是我的GitHub帐户中的一个示例,介绍了如何在ListView中自定义ArrayAdapter以合并ViewHolder。
我希望这会有帮助!

嗨,Isaac,我已经下载了你的项目,非常有用,看看你是如何让它与JSON一起工作的,这很有趣。在查看了array adapter类之后,是否可以更改你的代码,以便它查看我上面提到的数组?是的,这是可能的,我将在本周内查看它。好的,我已经看过了自从收到你的消息后,我就开始查看视图持有者,我想我已经解决了这个问题:DHi Isaac我已经下载了你的项目,它非常有用,看到你如何使用JSON很有趣。在查看了array adapter类之后,是否可以更改你的代码,以便它查看我上面提到的数组?是的,这是可能的,我将在本周看一看。好的,自从你的消息发布以来,我一直在查找视图持有者,我想我已经解决了这个问题:D