Chandra Sekhar
Chandra Sekhar

Reputation: 19502

Setting the List<Bitmap> to a GridView in Android

I am developing an app where I have a list of bitmaps and I want to place those bitmaps in a gridView. I have the folleowing code.

My Activity for gridView

public class TestBitmap extends Activity {

private Bitmap bitmap;
private ImageView image;
public static List<Bitmap> splittedBitmaps;

@Override
public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setContentView(R.layout.splitted_grid);
    splittedBitmaps = getIntent().getParcelableArrayListExtra("split");
    GridView gv = (GridView)findViewById(R.layout.splitted_grid);
    gv.setAdapter(new SplittedImageAdapter(this));
}
}

and this is my Adapter class

public class SplittedImageAdapter extends BaseAdapter{

private Context mContext;

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

@Override
public int getCount() {
    return TestBitmap.splittedBitmaps.size();
}

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

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

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView imageView = new ImageView(mContext);
    imageView.setLayoutParams(new GridView.LayoutParams(30,30));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(1, 1, 1, 1);
    //imageView.setImageResource(TestBitmap.splittedImages.get(arg0));
    imageView.setImageBitmap(TestBitmap.splittedBitmaps.get(arg0));
    return imageView;
}

}

I am getting a NullPointerException in the last line of the onCreate method of my Activity class. Please help me to trace the bug.

Upvotes: 1

Views: 2696

Answers (1)

Mariusz Jamro
Mariusz Jamro

Reputation: 31653

In last line of onCreate gb is null. That means (GridView)findViewById(R.layout.splitted_grid); is returning null, which means it is not finding the GridView.

I believe the argument of findViewById is incorrect. R.layout.splitted_grid is a name of layout file. Instead you should use the ID of a view, eg. R.id.your_view_id_here if you have ID specified in your layout XML like that:

<GridView
    android:id="@+id/your_view_id_here"
    (...)
/>

Upvotes: 1

Related Questions