2014-02-07 168 views
0

即時得到在我的代碼nullPointerException這是我的adapter如何設置位圖網格視圖

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 
    private Bitmap btimaprecieve; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return null; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, initialize some 
            // attributes 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     imageView.setImageResource(mThumbIds[position]); 

     return imageView; 
    } 

    private Integer[] mThumbIds = { R.drawable.sample_pic, 
      R.drawable.sample_pic, R.drawable.sample_pict, 
      R.drawable.sample_pict, R.drawable.sample_pic, }; 

    public Bitmap add(Bitmap bitmap_recieve) { 
     return btimaprecieve; 
    } 
} 

這是我的主要活動,其中我提到的GridView

 myImageAdapter= new ImageAdapter(this); 
     gridview.setAdapter(myImageAdapter); 
     myImageAdapter.add(Bitmap_recieve); 
+1

過去的日誌貓在哪兒買例外 – Nand

+0

首先格式的代碼.. –

回答

1

了NPE
public Object getItem(int position) { 
     return null; 
} 

因此,您應該替換此

public int getCount() { 
    return mThumbIds.length; 
} 

public Object getItem(int position) { 
    return null; 
} 

public long getItemId(int position) { 
    return 0; 
} 

隨着

@Override 
public int getCount() { 
    return mThumbIds.length; 
} 

@Override 
public Object getItem(int position) { 
    return mThumbIds[position]; 
} 

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

檢查您的getItem()和getItemId()方法,它應該是這樣的

@Override 
public Object getItem(int position) { 
    return mThumbIds.get(position); 
} 

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