title: Display a RecyclerView in Fragment
body: <p>I'm trying out the new <code>RecyclerView</code> in Android Lollipop and I'm stuck.</p>
  
  <p>I'm trying to receive a list, with an icon and a <code>TextView</code> to the right of the icon, inside a <code>Fragment</code>.</p>
  
  <p><a href="http://hmkcode.com/android-simple-recyclerview-widget-example/" rel="noreferrer">I found this great tutorial</a> on how to set up a <code>RecyclerView</code>. I have followed every point and only changed the <code>item_layout.xml</code> to fit my needs.</p>
  
  <p>The project builds without any errors but when it launches on my device I'm getting this error:</p>
  
  <blockquote>
    <p>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fredrikaldgard.materialcolors/com.fredrikaldgard.materialcolors.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference</p>
  </blockquote>
  
  <p>I've tried to google the problem but I'm quite an amateur with Android development.</p>
  
  <p>Here's my <code>MainActivity</code>:</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // 1. get a reference to recyclerView
      RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
  
      // 2. set layoutManger
      recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
      // this is data fro recycler view
      ItemData itemsData[] = { new ItemData("Indigo",R.drawable.circle),
              new ItemData("Red",R.drawable.color_ic_launcher),
              new ItemData("Blue",R.drawable.indigo),
              new ItemData("Green",R.drawable.circle),
              new ItemData("Amber",R.drawable.color_ic_launcher),
              new ItemData("Deep Orange",R.drawable.indigo)};
  
  
      // 3. create an adapter
      MyAdapter mAdapter = new MyAdapter(itemsData);
      // 4. set adapter
      recyclerView.setAdapter(mAdapter);
      // 5. set item animator to DefaultAnimator
      recyclerView.setItemAnimator(new DefaultItemAnimator());
  </code></pre>
  
  <p>And my <code>MyAdapter</code>:</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  private ItemData[] itemsData;
  
  public MyAdapter(ItemData[] itemsData) {
      this.itemsData = itemsData;
  }
  
  // Create new views (invoked by the layout manager)
  @Override
  public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
      // create a new view
      View itemLayoutView = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.item_layout, null);
  
      // create ViewHolder
  
      ViewHolder viewHolder = new ViewHolder(itemLayoutView);
      return viewHolder;
  }
  
  // Replace the contents of a view (invoked by the layout manager)
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int position) {
  
      // - get data from your itemsData at this position
      // - replace the contents of the view with that itemsData
  
      viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
      viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
  
  
  }
  
  // inner class to hold a reference to each item of RecyclerView
  public static class ViewHolder extends RecyclerView.ViewHolder {
  
      public TextView txtViewTitle;
      public ImageView imgViewIcon;
  
      public ViewHolder(View itemLayoutView) {
          super(itemLayoutView);
          txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
          imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
      }
  }
  
  
  // Return the size of your itemsData (invoked by the layout manager)
  @Override
  public int getItemCount() {
      return itemsData.length;
  }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> Here's the <code>Fragment</code></p>
  
  <pre><code>public class ColorsFragment extends Fragment {
  
      public ColorsFragment(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_colors, container, false);
  
          return rootView;
      }
  }
  </code></pre>
  
  <p>What has possibly gone wrong?</p>