2016-05-04 29 views
0

你好,我面臨的問題是,我有一個自定義列表視圖,其中包含7個位圖圖像,但當我試圖滾動我收到內存不足的錯誤,我有改變了所有的圖像大小,我的圖像都沒有超過50kb。我試圖改變清單內的堆,但我想知道是否有任何建議。我已經包含了提交給我的錯誤。Android在自定義列表視圖內存不足

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(); 
} 
} 

回答

0

我建議你使用滑翔或畢加索加載,緩存和管理圖像,也可以使用ViewHolder設計模式優化您的ListView性能並減少內存中的負載。基本上,ViewHolder所做的就是重複使用視圖而不增加新視圖(這增加了內存)。 這可能會降低你的記憶力。 Here是我的GitHub帳戶中有關如何自定義ListView中的ArrayAdapter以合併ViewHolder的示例。 我希望它有幫助!

+0

嗨艾薩克我已經下載了你的項目,這是非常有用的,它很有趣,看你如何得到它與J​​SON的工作。查看數組適配器類後,是否可以更改您的代碼,以便查看我上面列出的數組? – james

+0

是的,這是可能的,我會在週末看看 –

+0

那好吧,我一直在查看持有人,因爲你的消息,我想我已經排序的問題:D – james