Android this (context) giving NullPointerException
<p>Just doing a simple exercise by setting an adapter on a GridView: </p>

<pre><code>        GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
</code></pre>

<p>ImageAdapter is another class, as following:</p>

<pre><code>public class ImageAdapter extends BaseAdapter {

// ---the images to display---
public Integer[] imageIDs = { R.drawable.one, R.drawable.two,
        R.drawable.three, R.drawable.four, R.drawable.five,
        R.drawable.six };

private Context context;

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

// ---returns the number of images---
public int getCount() {
    return imageIDs.length;
}

// ---returns the ID of an item---
public Object getItem(int position) {
    return position;
}

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

// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(185, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(5, 20, 5, 5);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(imageIDs[position]);
    return imageView;
}
</code></pre>

<p>}</p>

<p>When I debug the code, app crashes on the gridview.setAdapter line, and report a <strong>java.lang.NullPointerException</strong> on <strong>this</strong>. Any ideas how this can be overcome?</p>
