title: How to display GridView inside a Fragment?
body: <p>I wish to display albums in grid with Album art, album name and number of songs. </p>
  
  <p>These are my xml layouts:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:id="@+id/content_frame"
  android:layout_height="match_parent"
  android:background="@color/metalList"&gt;
  
  
  &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/grid"
      android:layout_width="fill_parent"
      android:numColumns="auto_fit"
      android:layout_height="wrap_content"
      android:verticalSpacing="10dp"
      android:horizontalSpacing="10dp"
      android:stretchMode="columnWidth"
      android:layout_gravity="right|top"/&gt;
  
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>and for individual items:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout
  android:layout_width="160dp"
  android:layout_height="200dp"
  android:orientation="vertical"
  android:background="@color/metalList"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:layout_marginRight="5dp"
  android:elevation="4dp"
  xmlns:android="http://schemas.android.com/apk/res/android"&gt;
              &lt;ImageView
                  android:id="@+id/albumart"
                  android:layout_width="160dp"
                  android:layout_height="160dp"
                  android:layout_gravity="top"
                  android:layout_margin="0dp"
                  android:src="@drawable/musicicon"
                  /&gt;
              &lt;TextView
                  android:id="@+id/album_name"
                  android:layout_height="25dp"
                  android:layout_width="match_parent"
                  android:textColor="@color/metalTextB"
                  android:layout_below="@id/albumart"
                  android:layout_marginLeft="5dp"
                  android:textSize="16sp"
                  android:typeface="sans"
                  android:text="Album"
                  android:scrollHorizontally="true"
                  android:ellipsize="end"
                  android:maxLines="1"/&gt;
              &lt;TextView
                  android:id="@+id/songs_num"
                  android:layout_width="match_parent"
                  android:layout_height="15dp"
                  android:layout_below="@id/album_name"
                  android:layout_marginLeft="5dp"
                  android:textColor="@color/metalTextS"
                  android:textSize="10sp"
                  android:text="artist"
                  android:typeface="sans"
                  android:scrollHorizontally="true"
                  android:ellipsize="end"
                  android:maxLines="1"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here are the codes for the Fragment and Activity</p>
  
  <p><strong>Albums.java</strong>
      public class Albums extends Fragment implements LoaderManager.LoaderCallbacks {</p>
  
  <pre><code>AlbumsAdapter mAdapter;
  
  private static final String ARG_POSITION = "position";
  
  private int position;
  
  public static Albums newInstance(int position) {
      Albums f = new Albums();
      Bundle b = new Bundle();
      b.putInt(ARG_POSITION, position);
      f.setArguments(b);
      return f;
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View myFragmentView = inflater.inflate(R.layout.albums, container, false);
      GridView grid = (GridView) this.getActivity().findViewById(R.id.grid);
      mAdapter = new AlbumsAdapter(getActivity(), null);
      grid.setAdapter(mAdapter);
      return myFragmentView;
  }
  
  
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
  
      getLoaderManager().initLoader(0, null, this);
  }
  
  
  static final String[] ALBUM_SUMMARY_PROJECTION = { MediaStore.Audio.Albums._ID,MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ARTIST};
  
  
  
  public Loader&lt;Cursor&gt; onCreateLoader(int id, Bundle args) {
      String select = null;
      return new CursorLoader(getActivity(), MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,ALBUM_SUMMARY_PROJECTION, select, null, null);
  }
  
  
  
  public void onLoadFinished(Loader&lt;Cursor&gt; loader, Cursor data) {
      mAdapter.swapCursor(data);
  }
  
  
  
  public void onLoaderReset(Loader&lt;Cursor&gt; loader) {
      mAdapter.swapCursor(null);
  }
  }
  
  public class AlbumsAdapter extends CursorAdapter {
  
  private final LayoutInflater mInflater;
  
  public AlbumsAdapter(Context context, Cursor c) {
      super(context, c);
      mInflater = LayoutInflater.from(context);
  }
  
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      ImageView albumArt = (ImageView) view.findViewById(R.id.albumart);
      albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
      String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
      Bitmap img = BitmapFactory.decodeFile(path);
      if(img !=null)
          albumArt.setImageBitmap(img);
      else{
          Bitmap bit = getDefaultAlbumArt(context);
          albumArt.setImageBitmap(bit);
      }
  
      TextView albumTitle = (TextView) view.findViewById(R.id.album_name);
      albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
  
      TextView artistName = (TextView) view.findViewById(R.id.songs_num);
      artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));
  
  }
  
  
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      final View view = mInflater.inflate(R.layout.grid_item, parent, false);
      return view;
  }
  public Bitmap getDefaultAlbumArt(Context context) {
      Bitmap bm = null;
      BitmapFactory.Options options = new BitmapFactory.Options();
      try {
          bm = BitmapFactory.decodeResource(context.getResources(),
                  R.drawable.musicicon, options);
      } catch (Error ee) {
      } catch (Exception e) {
      }
      return bm;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>AlbumsAdapter.java</strong>
      public class AlbumsAdapter extends CursorAdapter {</p>
  
  <pre><code>private final LayoutInflater mInflater;
  
  public AlbumsAdapter(Context context, Cursor c) {
      super(context, c);
      mInflater = LayoutInflater.from(context);
  }
  
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      ImageView albumArt = (ImageView) view.findViewById(R.id.albumart);
      albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
      String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
      Bitmap img = BitmapFactory.decodeFile(path);
      if(img !=null)
          albumArt.setImageBitmap(img);
      else{
          Bitmap bit = getDefaultAlbumArt(context);
          albumArt.setImageBitmap(bit);
      }
  
      TextView albumTitle = (TextView) view.findViewById(R.id.album_name);
      albumTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
  
      TextView artistName = (TextView) view.findViewById(R.id.songs_num);
      artistName.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST)));
  
  }
  
  
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      final View view = mInflater.inflate(R.layout.grid_item, parent, false);
      return view;
  }
  public Bitmap getDefaultAlbumArt(Context context) {
      Bitmap bm = null;
      BitmapFactory.Options options = new BitmapFactory.Options();
      try {
          bm = BitmapFactory.decodeResource(context.getResources(),
                  R.drawable.musicicon, options);
      } catch (Error ee) {
      } catch (Exception e) {
      }
      return bm;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am getting a "java.lang.NullPointerException" with these codes.</p>