Android内存泄漏-Listview ImageView

Android内存泄漏-Listview ImageView,android,listview,memory-leaks,imageview,Android,Listview,Memory Leaks,Imageview,我有一个列表视图,它显示一些文本和图像。listview似乎工作正常。但是今天我意识到当我滚动listview时内存泄漏了 我的日志中有很多这样的警告: 08-12 17:06:15.037: D/dalvikvm(10826): GC_FOR_ALLOC freed 3499K, 28% free 36413K/49991K, paused 30ms, total 32ms 这是我的代码: public View getView(int position, View conve

我有一个列表视图,它显示一些文本和图像。listview似乎工作正常。但是今天我意识到当我滚动listview时内存泄漏了

我的日志中有很多这样的警告:

   08-12 17:06:15.037: D/dalvikvm(10826): GC_FOR_ALLOC freed 3499K, 28% free 36413K/49991K, paused 30ms, total 32ms
这是我的代码:

    public View getView(int position, View convertView, ViewGroup parent)
    {

        ViewHolder holder = null;           

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.event_item_typical, null);

              holder = new ViewHolder();

              holder.eventTitle = (TextView) convertView.findViewById(R.id.tv_EventTitle);
              holder.eventTypeName = (TextView) convertView.findViewById(R.id.tv_EventTypeName);
              holder.eventImg = (ImageView) convertView.findViewById(R.id.imgv_EventType);      
              holder.eventLocationName = (TextView) convertView.findViewById(R.id.tv_EventLocation);
              holder.eventDate = (TextView) convertView.findViewById(R.id.tv_EventDate);
              holder.eventTime = (TextView) convertView.findViewById(R.id.tv_EventTime);    
              holder.eventStatus = (TextView) convertView.findViewById(R.id.tv_EventStatus);    

              convertView.setTag(holder);
            }
        else {
              holder = (ViewHolder) convertView.getTag();
        }

        MyEvent cEvent = getItem(position);                 

        holder.eventTitle.setText(cEvent.getTitle());

        ActionTypes recordedType = cEvent.getActionType();
        if (recordedType != null)
        {                   
            holder.eventImg.setImageResource(recordedType.getImageId());
            holder.eventTypeName.setText(recordedType.getFullName());       
        }       

        holder.eventLocationName.setText(cEvent.getLocationName());
        holder.eventDate.setText(cEvent.getStartDate());
        holder.eventTime.setText(cEvent.getStartTime());
        holder.eventStatus.setText(cEvent.getStatus().name());

        if (cEvent.getStatus() == EventStatus.Active) {
            // holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.confirmed));
            holder.eventStatus.setBackgroundResource(R.color.confirmed); 
        } else if (cEvent.getStatus() == EventStatus.Expired){
            // holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.declined)); 
            holder.eventStatus.setBackgroundResource(R.color.declined); 
        } else if (cEvent.getStatus() == EventStatus.Cancelled){
            // holder.eventLocationName.setTextColor(MyContext.getResources().getColorStateList(R.color.pending)); 
            holder.eventStatus.setBackgroundResource(R.color.pending); 
        } 

        holder.eventStatus.setTypeface(null, Typeface.ITALIC);

        return convertView;
    }
我知道ImageView更新时会发生内存泄漏:holder.eventImg.setImageResourcerecordedType.getImageId

我在Drawables中访问图像

但是,如果我调用holder.eventImg.setImageResourceR.drawable.icon1,我不会得到这些内存泄漏。但我不能硬编码图像。我需要在列表项上显示不同的图像。请帮我解决这个内存泄漏问题

这是ActionType的代码。这是一个包含图像id的Enum类。我只发布Enum类中的一半项目

public enum ActionTypes {

Archery (ActionCategoty.Sports, "Archery", R.drawable.action_archery_250, null),
BabyShowerBoy (ActionCategoty.Social, "Baby Shower Boy", R.drawable.action_shower_boy_250, null),
BabyShowerGirl (ActionCategoty.Social, "Baby Shower Girl", R.drawable.action_babyshower_girl_250, null),
Badminton (ActionCategoty.Sports, "Badminton", R.drawable.action_batminton43, null),
Ballooning (ActionCategoty.Hobbies, "Ballooning", R.drawable.action_balooning_250, null),
Baseball (ActionCategoty.Sports,"Baseball", R.drawable.action_baseball_28, null),
Basketball (ActionCategoty.Sports,"Basketball", R.drawable.action_basketball6, null),   
Bbq (ActionCategoty.Social,"BBQ", R.drawable.action_bbq_250, null), 
Bowling (ActionCategoty.Sports,"Bowling", R.drawable.action_bowling_15, null),
Boxing (ActionCategoty.Sports,"Boxing", R.drawable.action_boxing_32, null),

private  ActionCategoty category; // Id of the team for the action    
private String fullName; // Complete name of the action
private final int imageId; // Id of the logo of the sport image in the resources
private  String teamId; // Id of the team for the action    
private ActionSkillLevel skillevel;


private ActionTypes(ActionCategoty _category, String _fullName, int _imageId, String _teamId) {
    this.setCategory(_category);
    this.fullName = _fullName;
    this.imageId = _imageId;
    this.teamId = _teamId;
}

public String getFullName(){
     return fullName;
}


public int getImageId() {
    return imageId;
}

public String getTeamId() {
    if (teamId == null)
    {
        teamId = "null";
    }
    return teamId;
}

public void setTeamId(String _teamId) {
    this.teamId = _teamId;
}


public ActionCategoty getCategory() {
    return category;
}

public void setCategory(ActionCategoty category) {
    this.category = category;
}


public ActionSkillLevel getSkillevel() {
    if (this.skillevel == ActionSkillLevel.X){
        this.skillevel = null;
    }
    return skillevel;
}

public void setSkillevel(ActionSkillLevel skillevel) {
    if (skillevel != ActionSkillLevel.X){
        this.skillevel = skillevel;
    } else {
        this.skillevel = null;
    }
}


// Category 
public enum ActionCategoty {Sports, Games, Social , Hobbies, Fitness;}    

// Skill Level
public enum ActionSkillLevel {  X("Select Your Skill Level"),
                                A("A - Professional"),
                                B("B - Elite"),
                                C("C - Advanced"),
                                D("D - Intermediate"),
                                E("E - Beginner");    
private String definition;

private ActionSkillLevel(String _definition) {
    this.definition = _definition;
}

public String getDefinition(){
 return definition;
}   


}
}

图片的大小和重量是多少,屏幕上可以看到多少?你能发布你的ActionType代码吗?谢谢回复。图像的大小约为250 x 250。重量约为6KB。user2713030,我一次在屏幕上显示大约5到30个listview项目。为什么您认为内存泄漏?该消息在我看来就像一条普通的GC消息。你可以分析你的记忆,看看当你滚动列表时它是否变大。