你好我面临的问题是我有一个自定义列表视图,其中包含7个Bitmap图像但是当我尝试滚动时收到内存不足错误,我已经更改了所有图像大小而没有我的图像甚至高于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();
}
}
答案 0 :(得分:0)
我建议您使用Glide或Picasso来加载,缓存和管理图像,还可以使用ViewHolder设计模式来优化ListView性能并减少内存中的负载。基本上,ViewHolder所做的是重用视图而不会膨胀新的视图(这会增加内存)。 这可能会降低你的记忆力。 Here是我的GitHub帐户中有关如何在ListView中自定义ArrayAdapter以合并ViewHolder的示例。 我希望它有所帮助!