title: Android Studio: RecyclerView not displaying data
body: <p>I am currently making an Android application with a feed, so I can display a little blog using custom data from QuickBlox. I was wanting each record to appear in a CardView with a picture and a title underneath it, then when the user taps the card, it expands to show more details about it. However, my attempts have always ended up with it not displaying the cards on the RecyclerView. I do have data that it downloads, but I do not know why it isn't showing up.</p>
  
  <p>The fragment that holds the main code is <code>HomeRecyclerViewFragment</code>, the adapter is <code>CustomAdapter</code>, and the layout XML is <code>fragment_home.xml</code></p>
  
  <p>HomeRecyclerViewFragment:</p>
  
  <pre><code>public class HomeRecyclerViewFragment extends Fragment {
  
  private static final String TAG = "HomeRecyclerViewFragment";
  private static final String KEY_LAYOUT_MANAGER = "layoutManager";
  private static final int SPAN_COUNT = 2;
  private static final int DATASET_COUNT = 60;
  private QBCustomObject qbco;
  public static ProgressDialog dia;
  HashMap&lt;String, Object&gt; fields = new HashMap&lt;String, Object&gt;();
  public List&lt;String&gt; ids;
  Handler myHandler;
  
  private enum LayoutManagerType {
      GRID_LAYOUT_MANAGER,
      LINEAR_LAYOUT_MANAGER
  }
  
  protected LayoutManagerType mCurrentLayoutManagerType;
  
  protected CardView mLinearLayoutRadioButton;
  protected CardView mGridLayoutRadioButton;
  
  protected RecyclerView mRecyclerView;
  protected CustomAdapter mAdapter;
  protected RecyclerView.LayoutManager mLayoutManager;
  protected String[] mDataset;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      // Initialize dataset, this data would usually come from a local content provider or
      // remote server.
      dia = ProgressDialog.show(this.getContext(), null,"Downloading recent posts...");
      dia.show();
      initDataset();
      myHandler = new Handler();
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_home, container, false);
      rootView.setTag(TAG);
  
      mRecyclerView = (RecyclerView) rootView.findViewById(R.id.homerecyclerview);
  
      // LinearLayoutManager is used here, this will layout the elements in a similar fashion
      // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
      // elements are laid out.
      mLayoutManager = new LinearLayoutManager(getActivity());
  
      mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
  
      if (savedInstanceState != null) {
          // Restore saved layout manager type.
          mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
                  .getSerializable(KEY_LAYOUT_MANAGER);
      }
      setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
  
      mAdapter = new CustomAdapter(mDataset);
      // Set CustomAdapter as the adapter for RecyclerView.
      //mRecyclerView.setAdapter(mAdapter);
  
      mLinearLayoutRadioButton = (CardView) rootView.findViewById(R.id.card_view);
      mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER);
          }
      });
      //mRecyclerView.setAdapter(mAdapter);
  
      updateUI();
      return rootView;
  }
  
  /**
   * @param layoutManagerType Type of layout manager to switch to.
   */
  public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
      int scrollPosition = 0;
  
      // If a layout manager has already been set, get current scroll position.
      if (mRecyclerView.getLayoutManager() != null) {
          scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
                  .findFirstCompletelyVisibleItemPosition();
      }
  
      switch (layoutManagerType) {
          case GRID_LAYOUT_MANAGER:
              mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
              mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
              break;
          case LINEAR_LAYOUT_MANAGER:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
              break;
          default:
              mLayoutManager = new LinearLayoutManager(getActivity());
              mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
      }
  
      mRecyclerView.setLayoutManager(mLayoutManager);
      mRecyclerView.scrollToPosition(scrollPosition);
  }
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
      // Save currently selected layout manager.
      savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
      super.onSaveInstanceState(savedInstanceState);
  }
  
  
  private void initDataset() {
  
      QBCustomObjects.getObjects("posts", (QBCallback) qbco, new QBCallbackImpl() {
          @Override
          public void onComplete(Result result) {
              if (result.isSuccess()) {
                  Log.i(this.toString(),"Retrieved posts successfully.");
                  dia.dismiss();
                  QBCustomObjectLimitedResult coresult = (QBCustomObjectLimitedResult) result;
                  ArrayList&lt;QBCustomObject&gt; co = coresult.getCustomObjects();
  
  
                  int sum = 0;
  
                  for (int i = 0; i &lt; co.size(); ++i) {
                      ids.add(co.get(i).getCustomObjectId());
                      sum += Arrays.toString(co.toArray()).toCharArray()[i];
                  }
                  String[] stockArr = new String[ids.size()];
  
                  stockArr = ids.toArray(stockArr);
                  mDataset = stockArr;
  
                  updateUI();
              } else {
                  dia.dismiss();
                  Log.e("Errors",result.getErrors().toString());
              }
          }
      });
      dia.dismiss();
  }
  
  private void updateUI() {
      mAdapter = new CustomAdapter(mDataset);
      mRecyclerView.setAdapter(mAdapter);
  }
  }
  </code></pre>
  
  <p>CustomAdapter:</p>
  
  <pre><code>public class CustomAdapter extends RecyclerView.Adapter&lt;CustomAdapter.MyViewHolder&gt; {
  private String[] mDataset;
  private Uri[] imgUriList;
  private List&lt;String&gt; listOfTitles;
  private QBCustomObject qbco;
  public ProgressDialog dia;
  HashMap&lt;String, Object&gt; fields = new HashMap&lt;String, Object&gt;();
  public List&lt;String&gt; ids;
  
  
  private String id;
  private String name;
  private String description;
  private String year;
  private float rating;
  private long date;
  
  Handler myHandler;
  
  // Provide a reference to the views for each data item
  // Complex data items may need more than one view per item, and
  // you provide access to all the views for a data item in a view holder
  public static class MyViewHolder extends RecyclerView.ViewHolder {
      public CardView mCardView;
      public TextView mTextView;
      public MyViewHolder(View v) {
          super(v);
  
          mCardView = (CardView) v.findViewById(R.id.card_view);
          mTextView = (TextView) v.findViewById(R.id.info_text);
  
      }
  }
  
  // Provide a suitable constructor (depends on the kind of dataset)
  public CustomAdapter(String[] myDataset) {
      mDataset = myDataset;
  
  }
  
  // Create new views (invoked by the layout manager)
  @Override
  public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
      // create a new view
      View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.fragment_home, parent, false);
      // set the view's size, margins, paddings and layout parameters
      MyViewHolder vh = new MyViewHolder(v);
      dia = ProgressDialog.show(v.getContext(), null,"Downloading recent posts...");
      dia.show();
      myHandler = new Handler();
      //initQb();
  
      return vh;
  
  }
  
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
      holder.mTextView.setText(mDataset[position]);
  }
  
  @Override
  public int getItemCount() {
  
      if (mDataset != null) return mDataset.length;
      else return 0;
  }
  
  public void initQb() {
      QBCustomObjects.getObjects("posts", (QBCallback) qbco, new QBCallbackImpl() {
          @Override
          public void onComplete(Result result) {
              if (result.isSuccess()) {
                  QBCustomObjectLimitedResult coresult = (QBCustomObjectLimitedResult) result;
                  ArrayList&lt;QBCustomObject&gt; co = coresult.getCustomObjects();
                  dia.dismiss();
                  Log.println(Log.INFO,"PostGetHandler","Retrieved posts successfully.");
                  int sum = 0;
  
                  for (int i = 0; i &lt; co.size(); ++i) {
                      ids.add(co.get(i).getCustomObjectId().toString());
                      sum += co.toArray().toString().toCharArray()[i];
                  }
                  String[] stockArr = new String[ids.size()];
                  stockArr = ids.toArray(stockArr);
                  CustomAdapter ca = new CustomAdapter(stockArr);
  
              } else {
                  dia.dismiss();
                  Log.e("Errors",result.getErrors().toString());
              }
          }
      });
  }
  }
  </code></pre>
  
  <p>fragment_home.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:weightSum="1"
  android:id="@+id/linearlayoutv"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:card_view="http://schemas.android.com/apk/res-auto"
  android:background="@drawable/splashscreen"&gt;
  
  
  
  &lt;android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/homerecyclerview"&gt;    &lt;/android.support.v7.widget.RecyclerView&gt;
  &lt;android.support.v7.widget.CardView
      android:id="@+id/card_view"
      android:layout_width="match_parent"
      card_view:cardCornerRadius="4dp"
      android:layout_weight="0.25"
      android:layout_height="match_parent"
      android:layout_marginLeft="5dp"
      android:layout_marginRight="5dp"
      android:layout_marginTop="5dp"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="212dp"
          android:orientation="horizontal"&gt;
  
          &lt;TextView
              android:id="@+id/info_text"
              android:layout_width="347dp"
              android:layout_height="34dp"
              android:textSize="16sp"
              android:foregroundGravity="bottom"
              android:gravity="bottom"
              android:layout_marginTop="180dp" /&gt;
  
          &lt;ImageView
              android:id="@+id/myImage"
              android:layout_width="wrap_content"
              android:layout_gravity="fill_horizontal"
              android:layout_height="wrap_content" /&gt;
  
  
      &lt;/LinearLayout&gt;
  &lt;/android.support.v7.widget.CardView&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated. Thanks!</p>
  
  <p>Edit 1:
  New error log (Scrolling to the right shows the rest)</p>
  
  <pre><code>FATAL EXCEPTION: main
                                                                                  Process: com.example.adamangle.ameroboticsapp, PID: 2977
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                                      at io.github.adamangle.ameroboticsapp.fragments.HomeRecyclerViewFragment.onCreateView(HomeRecyclerViewFragment.java:105)
                                                                                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
                                                                                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>