2012-08-17 78 views
1

如果我們快速滾動Listview,應用程序會拋出致命異常OutofMemoryError滾動ListView時拋出OutofMemoryException

它滾動時總是給出相同的錯誤。如何處理它?

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) 
+0

你的例外沒有了嗎? – 2012-09-26 11:13:49

回答

-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>; 
} 
+0

你的回答是絕對錯誤的,因爲你說從你的角度來看所有東西都返回相同的'int變量'。 – 2012-08-17 04:44:27

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; 
    } 
0

您還可以覆蓋活動 onLowMemory()方法,而當活動檢測低內存運行垃圾收集器。 您可以使用System.gc()。也可以在此方法中取消不需要的變量。

相關問題