2012-01-18 8 views
-1

私はGridViewコントロールで画像をペイントこのクラスを持っている:ペイント画像

public class ImageAdapter extends BaseAdapter 
{ 
    Context MyContext; 
    Vector<User> users; 
    int level; 


    public ImageAdapter(Context _MyContext, Vector<User> _users, int _level) 
    { 
     MyContext = _MyContext; 
     users = _users; 
     level = _level; 
    } 

    @Override 
    public int getCount() 
    { 
        /* Set the number of element we want on the grid */ 
     if(users==null) 
      return 0; 
     return users.size(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View MyView = convertView;    
          /*we define the view that will display on the grid*/ 
     //Inflate the layout 
     LayoutInflater li = getLayoutInflater(); 
     MyView = li.inflate(R.layout.grid_item, null); 

     // Add The Text!!! 
     TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text); 

     // Add The Image!!!   
     ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image); 
     iv.setBackgroundResource(R.drawable.button_restricted); 
     iv.setImageResource(R.drawable.defaultperson02); 
     byte[] bytes; 
     if(position<users.size()){ 
      final User user =users.get(position); 
      tv.setText(users.get(position).getDisplay_name()); 
      bytes = user.getAvatar(); 
      if(bytes!=null && bytes.length>0){ 
       try{ 
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
        iv.setImageBitmap(bitmap); 
       }catch(Exception e){       

       } 
      } 

     } 

     return MyView; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     return 0; 
    } 
} 

このクラスは、通常は完璧に動作しますが、時々、私はこの例外を取得:

01-18 12:39:26.160: ERROR/AndroidRuntime(30496): FATAL EXCEPTION: main 
01-18 12:39:26.160: ERROR/AndroidRuntime(30496): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=15303KB, Allocated=10747KB, Bitmap Size=18391KB) 
01-18 12:39:26.160: ERROR/AndroidRuntime(30496):  at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 
01-18 12:39:26.160: ERROR/AndroidRuntime(30496):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:625) 
01-18 12:39:26.160: ERROR/AndroidRuntime(30496):  at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:638) 

私はリサイクルを試してみましたビットマップが表示されますが、エラーは引き続き表示されます。

は、私はこの問題を持っているなぜあなたはどんな考えを持っていますか?

答えて

0

あなたはconvertviewクラスを再利用する必要があります。上記のコードで何をしているのかは、getViewが呼び出されるたびに新しいビューを作成することです。これは正しいことではありません。代わりにconvertviewがnullかどうかを確認してください。 nullの場合はビューを拡張し、変換ビューを再利用します。詳細はthisをご確認ください。

関連する問題