Create a GridView of images inside a Fragment
<p>I am trying to create a Grid View inside a Fragment. I have implemented the adaptor class and the respective overriden methods. But while running it throws an exception in the line :</p>

<pre><code>gridView.setOnItemClickListener (this);
</code></pre>

<p>stating that the gridView object is a NULL pointer.</p>

<p>Here is my code (very similar to <a href="https://developer.android.com/guide/topics/ui/layout/gridview.html" rel="nofollow">Google Sample</a>) :</p>

<pre><code>public static class OcrOverrideFragment extends Fragment implements AdapterView.OnItemClickListener {

    // The Adaptor Class for the grid view
    public class ImageAdaptor extends BaseAdapter{

        private Context mContext;

        public ImageAdaptor (Context c)
        {
            mContext = c;

        }

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

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

        public int getCount (){

            try
            {
                String[] f = mContext.getAssets().list("OcrSampleImages");
                return f.length;
            }
            catch (IOException e)
            {
                LogUtil.d (TAG,"OCR_OVERRIDE gave " + e);
            }
            return 0;
        }

        // For each of the items in the grid, this method will be called
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView imageView = null;

            try
            {
                String[] f = mContext.getAssets().list("OcrSampleImages");

                if (convertView == null) {
                    imageView = new ImageView(getContext());
                    imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setPadding(8, 8, 8, 8);
                }
                else {
                    imageView = (ImageView) convertView;
                }

                // Setting the appropriate image for each gird item
                imageView.setImageBitmap(getBitmapFromAsset("OcrSampleImages"+System.getProperty("file.separator")+f[position]));

            }
            catch (IOException e)
            {
                LogUtil.d (TAG,"OCR_OVERRIDE gave " + e);
            }

            return imageView;
        }
    } // End Of ImageAdaptor Class

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.ocr_override, container, false);

            GridView gridView = (GridView) (getActivity().findViewById(R.id.ocrgridview));
            gridView.setAdapter(new ImageAdaptor(v.getContext()));
            gridView.setOnItemClickListener (this);

        return v;
    }

    @Override
    public void onItemClick (AdapterView&lt;?&gt; adaptorView,View v,int i,long l)
    {
        // Implementation details unncessary

    }
</code></pre>