Out of memory on a byte allocation

后端 未结 3 404
忘掉有多难
忘掉有多难 2021-01-15 21:59

This is the error I am receiving on the Android log, exactly, this is:

08-06 12:16:28.763: E/dalvikvm-heap(27065): Out of memory on a 184-byte allocation.
         


        
3条回答
  •  一生所求
    2021-01-15 22:38

    Since you are using a listActivity check if you have implemented the optimizations shown here.

    I solved a similar issue with list view by implementing the optimization

    Here are some excerpts from the presentation about optimizing a listAdapter

    The Slow way

    public View getView(int position, View convertView, ViewGroup parent) { 
         View item = mInflater.inflate(R.layout.list_item_icon_text, null);
         ((TextView) item.findViewById(R.id.text)).setText(DATA[position]); 
         ((ImageView) item.findViewById(R.id.icon)).setImageBitmap( 
                 (position & 1) == 1 ? mIcon1 : mIcon2);
         return item; 
    }
    

    The Proper way

     public View getView(int position, View convertView, ViewGroup parent) { 
         if (convertView == null) { 
             convertView = mInflater.inflate(R.layout.item, parent, false); 
         } 
         ((TextView) convertView.findViewById(R.id.text)).setText(DATA[position]); 
         ((ImageView) convertView.findViewById(R.id.icon)).setImageBitmap( 
                 (position & 1) == 1 ? mIcon1 : mIcon2); 
         return convertView; 
     }
    

    The Best Way

    static class ViewHolder { 
            TextView text; 
            ImageView icon; 
    }
    
     public View getView(int position, View convertView, ViewGroup parent) { 
             ViewHolder holder; 
    
             if (convertView == null) { 
                 convertView = mInflater.inflate(R.layout.list_item_icon_text, 
                         parent, false);
                 holder = new ViewHolder(); 
                 holder.text = (TextView) convertView.findViewById(R.id.text); 
                 holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
    
                convertView.setTag(holder); 
            } else { 
                holder = (ViewHolder) convertView.getTag(); 
            } 
    
            holder.text.setText(DATA[position]); 
            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 
    
            return convertView; 
        }
    

提交回复
热议问题