1

如果我们Listview快速滚动,应用程序会抛出一个致命异常OutofMemoryError

滚动时它总是给出相同的错误。如何处理?

public class UpcomingAdapter extends BaseAdapter{
    Typeface typeFace;
    private ArrayList<String> mEventName,mDate,mTime;
    private LayoutInflater mInflater;

    public UpcomingAdapter(LayoutInflater mInflater,
                           ArrayList<String> mDEventName,
                           ArrayList<String> mDdate,
                           ArrayList<String> mDtime,
                           Typeface typeface)
    {
        this.mEventName=mDEventName;
        this.mDate=mDdate;
        this.mTime=mDtime;       
        this.mInflater=mInflater;
        this.typeFace=typeface;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View view, ViewGroup parent) {
        ViewHolder holder = null;

        if(view==null) {
            holder=new ViewHolder();
            view=mInflater.inflate(R.layout.upcomingevent_list,null);
            view.setTag(holder);
            holder=(ViewHolder) view.getTag();
            holder.holderDate=(TextView)view.findViewById(R.id.xTvUpcomingEvents_DateList);
            holder.holderEventName=(TextView)view.findViewById(R.id.xTvUpcomingEvents_Events);
            holder.holderEventName.setTypeface(typeFace);
            holder.holderDate.setTypeface(typeFace);
        } else {
            holder=(ViewHolder) view.getTag();  
        }

        //holder.employeeName.setTag(mEmpIds.get(arg0));
        setText(mEventName.get(arg0), holder.holderEventName);
        setText(mDate.get(arg0), holder.holderDate);
        setText(mTime.get(arg0), holder.holderEventName);

        holder.holderEventName.setTag(mEventName.get(arg0));
        holder.holderEventName.setTypeface(typeFace);

        //holder.holderArtistId.setTypeface(typeFace);

        return view;
    }

    public static class ViewHolder{
        TextView holderEventName;
        TextView holderDate;

        //TextView holderArtistId;
    }

    private void setText(String data,TextView view){
        if(data.equals(StaticUtils.sNullString)){
            view.setText(StaticUtils.sEmptyString);
        } else {
            view.setText(data);
        }
    }
}

这是错误:

08-17 15:08:22.950: E/AndroidRuntime(25423): FATAL EXCEPTION: main
08-17 15:08:22.950: E/AndroidRuntime(25423): java.lang.OutOfMemoryError: [memory exhausted]
08-17 15:08:22.950: E/AndroidRuntime(25423):    at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

在您的 getView 方法中尝试:

@Override
    public View getView(int arg0, View view, ViewGroup parent) {


        if(view==null) {
            LayoutInflater layoutInflater=LayoutInflater.from(getContext());
            view=layoutInflater.inflate(R.layout.upcomingevent_list, parent, false);
            holder=new ViewHolder();
            holder.holderDate=(TextView)view.findViewById(R.id.xTvUpcomingEvents_DateList);
            holder.holderEventName=(TextView)view.findViewById(R.id.xTvUpcomingEvents_Events);
            holder.holderEventName.setTypeface(typeFace);
            holder.holderDate.setTypeface(typeFace);
            view.setTag(holder);
        } else {
            holder=(ViewHolder) view.getTag();  
        }

        //holder.employeeName.setTag(mEmpIds.get(arg0));
        setText(mEventName.get(arg0), holder.holderEventName);
        setText(mDate.get(arg0), holder.holderDate);
        setText(mTime.get(arg0), holder.holderEventName);

        holder.holderEventName.setTag(mEventName.get(arg0));
        holder.holderEventName.setTypeface(typeFace);

        //holder.holderArtistId.setTypeface(typeFace);

        return view;
    }
于 2012-08-22T05:04:54.420 回答
0

您还可以覆盖活动的 onLowMemory() 方法,并在活动检测到内存不足时运行垃圾收集器。您可以使用 System.gc()。还要取消此方法中不需要的变量。

于 2013-03-08T08:25:41.203 回答
-2

用您现有的代码替换以下方法。

   @Override
        public int getItem(int position) {
            return <Return Particular int variable from Data>;
        }

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return <Return Particular int variable from Data>;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return <Return Particular int variable from Data>;
}
于 2012-08-17T04:36:32.773 回答