【问题标题】:OutOfMemory Error in Custom ListView Adapter [Mono Android]自定义 ListView 适配器 [Mono Android] 中的 OutOfMemory 错误
【发布时间】:2012-12-03 14:26:40
【问题描述】:

您好 Stack Overflow Android 用户,

请帮忙解答以下问题:

问题

我正在为一个列出鱼类种类的应用程序编写一个功能。我正在为适配器使用自定义 ListView 适配器类(我们称之为 FishSpeciesListAdapter)。到目前为止,我已将 27 种鱼类记录为该程序的默认值(您最终也可以自己添加一些)。问题是,当我将适配器链接到实际的 listview xml 对象并向下滚动列表时,过了一会儿,我收到以下错误:

UNHANDLED EXCEPTION: Java.Lang.OutOfMemoryError: Exception of type 'Java.Lang.OutOfMemoryError' was thrown.
at Android.Runtime.JNIEnv.CallStaticObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00080>
at Android.Graphics.BitmapFactory.DecodeByteArray (byte[],int,int) <0x001db>
at FishinTales.FishSpeciesListAdapter.GetView (int,Android.Views.View,Android.Views.ViewGroup) <0x002f7>
at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (intptr,intptr,int,intptr,intptr) <0x00093>
at (wrapper dynamic-method) object.5dcc133f-a229-41c4-80e2-f5f5260ecdad (intptr,intptr,int,intptr,intptr) <0x0004b>

  --- End of managed exception stack trace ---
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:392)
    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
    at fishintales.FishSpeciesListAdapter.n_getView(Native Method)
    at fishintales.FishSpeciesListAdapter.getView(FishSpeciesListAdapter.java:55)
    at android.widget.AbsListView.obtainView(AbsListView.java:1294)
    at android.widget.ListView.addViewBelow(ListView.java:2896)
    at android.widget.ListView.scrollListItemsBy(ListView.java:2825)
    at android.widget.ListView.arrowScrollImpl(ListView.java:2322)
    at android.widget.ListView.arrowScroll(ListView.java:2269)
    at android.widget.ListView.commonKey(ListView.java:2071)
    at android.widget.ListView.onKeyDown(ListView.java:2018)
    at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
    at android.view.View.dispatchKeyEvent(View.java:3740)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:786)
    at android.widget.ListView.dispatchKeyEvent(ListView.java:2003)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
    at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
    at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

Unhandled Exception:
Java.Lang.OutOfMemoryError: Exception of type 'Java.Lang.OutOfMemoryError' was thrown.
at Android.Runtime.JNIEnv.CallStaticObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x00080>
at Android.Graphics.BitmapFactory.DecodeByteArray (byte[],int,int) <0x001db>
at FishinTales.FishSpeciesListAdapter.GetView (int,Android.Views.View,Android.Views.ViewGroup) <0x002f7>
at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (intptr,intptr,int,intptr,intptr) <0x00093>
at (wrapper dynamic-method) object.5dcc133f-a229-41c4-80e2-f5f5260ecdad (intptr,intptr,int,intptr,intptr) <0x0004b>

  --- End of managed exception stack trace ---
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:392)
    at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
    at fishintales.FishSpeciesListAdapter.n_getView(Native Met

这是我的自定义 ListAdapter:

public class FishSpeciesListAdapter : BaseAdapter
{
    Context n_context;
    List<AppCode.Specie> n_specieData;

    public FishSpeciesListAdapter (Context context, List<AppCode.Specie> specieData)
    {
        this.n_context = context;
        this.n_specieData = specieData;
    }

    public override int Count {
        get { return this.n_specieData.Count; }
    }

    public override Java.Lang.Object GetItem (int position)
    {
        return null;
    }

    public override long GetItemId (int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        View v;
        if(convertView==null){

            LayoutInflater li = LayoutInflater.FromContext(parent.Context);
            v = li.Inflate(Resource.Layout.Adapter_FishSpeciesIcon, null);
        }
        else
        {
            v = convertView;
        }
        ImageView iconImage = (ImageView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesIconImage);
        TextView nameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesNameText);
        TextView scientificNameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesScientificNameText);

        nameText.Text = this.n_specieData[position].Name;
        scientificNameText.Text = this.n_specieData[position].ScientificName;

        if (this.n_specieData[position].RelatedMedia.AttachedPhotos.Count < 1)
        {
            iconImage.SetImageResource(Resource.Drawable.Icon); 
        }
        else
        {
            iconImage.SetImageBitmap(BitmapFactory.DecodeByteArray(this.n_specieData[position].RelatedMedia.AttachedPhotos[0], 0, this.n_specieData[position].RelatedMedia.AttachedPhotos[0].Length));  
        }   

        return v;
    }
}

代表我的其他研究

所有图片都在 100-300KB 的范围内。 这真的会导致这个问题吗?

reference my last post 获取更多代码,因为它有点相关但需要一个新问题。感谢您的时间和帮助。

【问题讨论】:

  • 你的图片有多大(以像素为单位)?
  • 图像的像素大小范围从 '347px x 222px' 到 '769px x 325px'
  • 你可能想在加载它们时调整它们的大小
  • 我注意到将所有图像加载到我的主“MyApp”类中确实需要一些时间。它成功地将所有内容加载到字节数组中并正确加载列表视图屏幕,当我向下滚动到最大尺寸的图像(301KB)时,我收到错误并且永远无法在列表视图中看到图像。它们都是 PNG 文件,因为我需要这种类型的图像本身的透明背景。我会尝试先制作最大的文件,所以理论上,当我下次运行它时,它应该在屏幕加载时立即失败,我什至什么都看不到。
  • 我会考虑一劳永逸地将图像加载到数组中

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


【解决方案1】:

解决了!

它所做的是在每次调用 GetView 时将字节数组解码为位图。所以我所做的就是在构造函数中将它们全部转换,并在调用 GetView 方法时制作位图缓存以供参考。

这是我更新的自定义 ListView 适配器。

public class FishSpeciesListAdapter : BaseAdapter
{
    Context n_context;
    List<AppCode.Specie> n_specieData;
    List<Bitmap> n_bitmapCache;

    public FishSpeciesListAdapter (Context context, List<AppCode.Specie> specieData)
    {
        this.n_context = context;
        this.n_specieData = specieData;
        this.n_bitmapCache = new List<Bitmap>();
        this.LoadBitmapsIntoCache();
    }

    private void LoadBitmapsIntoCache()
    {
        foreach(AppCode.Specie specie in this.n_specieData)
        {
            if (specie.RelatedMedia.AttachedPhotos.Count < 1)
            {
                this.n_bitmapCache.Add(BitmapFactory.DecodeResource(this.n_context.Resources, Resource.Drawable.Icon)); 
            }
            else
            {
                this.n_bitmapCache.Add(BitmapFactory.DecodeByteArray(specie.RelatedMedia.AttachedPhotos[0], 0, specie.RelatedMedia.AttachedPhotos[0].Length));  
            }
        }
    }

    public override int Count {
        get { return this.n_specieData.Count; }
    }

    public override Java.Lang.Object GetItem (int position)
    {
        return null;
    }

    public override long GetItemId (int position)
    {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        if(convertView==null){

            LayoutInflater li = LayoutInflater.FromContext(parent.Context);
            convertView = li.Inflate(Resource.Layout.Adapter_FishSpeciesIcon, null);
        }

        ImageView iconImage = (ImageView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesIconImage);
        TextView nameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesNameText);
        TextView scientificNameText = (TextView)convertView.FindViewById(Resource.Id.xml_adapter_fishSpeciesScientificNameText);

        nameText.Text = this.n_specieData[position].Name;
        scientificNameText.Text = this.n_specieData[position].ScientificName;
        iconImage.SetImageBitmap(this.n_bitmapCache[position]); 

        return convertView;
    }
}

在构造函数中调用 LoadBitmapsIntoCache() 并将位图保存到列表后。问题已解决,我可以滚动浏览所有鱼类,而不会出现任何 OutOfMemory 错误。感谢所有帮助过的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多