title: Android - RecyclerView inside of Fragment - NullPointerException
body: <p>I'm trying to use a <code>RecyclerView</code> inside of a <code>Fragment</code> (to use it in a <code>ViewPager</code>) but I got a <code>NullPointerException</code> when I try to execute:</p>
  
  <pre><code> Process: com.yomansk8.birthdaysoundboard, PID: 4448
  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.onMeasure(android.support.v7.widget.RecyclerView$Recycler, android.support.v7.widget.RecyclerView$State, int, int)' on a null object reference
          at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at android.view.View.measure(View.java:17430)
          at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
          at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
          at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
          at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
          at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
          at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
          at android.view.View.measure(View.java:17430)
          at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
          at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
          at android.view.Choreographer.doCallbacks(Choreographer.java:580)
          at android.view.Choreographer.doFrame(Choreographer.java:550)
          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
          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>
  
  <p>First, this is the layout of my fragment, it's only containing a <code>RecyclerView</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/recyclerDivers"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingTop="?attr/actionBarSize"
  android:clipToPadding="false"/&gt;
  </code></pre>
  
  <p>This is my Fragment:</p>
  
  <pre><code>package com.yomansk8.birthdaysoundboard.Fragments;
  
  import android.app.Activity;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.ActionBar;
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import com.yomansk8.birthdaysoundboard.Adapters.RecyclerViewAdapter;
  import com.yomansk8.birthdaysoundboard.GenerateDatas;
  import com.yomansk8.birthdaysoundboard.MainActivity;
  import com.yomansk8.birthdaysoundboard.Models.ViewModel;
  import com.yomansk8.birthdaysoundboard.R;
  import com.yomansk8.birthdaysoundboard.ScrollManager;
  
  import java.util.List;
  
  /**
   * Created by Yohann on 09/03/2015.
   */
  public class DiversFragment extends Fragment implements  RecyclerViewAdapter.OnItemClickListener {
  
  private MediaPlayer mPlayer = null;
  private Toolbar toolbar;
  private RecyclerView recyclerView;
  private GridLayoutManager mLayoutManager;
  private RecyclerViewAdapter adapter;
  private  List&lt;ViewModel&gt; items;
  
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View v = inflater.inflate(R.layout.fragment_divers, container, false);
  
      toolbar = ((MainActivity)getActivity()).getToolbar();
  
      recyclerView = (RecyclerView) v.findViewById(R.id.recyclerDivers);
      recyclerView.setHasFixedSize(true);
  
      items = GenerateDatas.getAmbianceList();
  
  
      adapter = new RecyclerViewAdapter(items);
  
  
      /*toolbar.post(new Runnable() {
          @Override public void run() {
              ScrollManager manager = new ScrollManager();
              manager.attach(recyclerView);
              manager.addView(toolbar, ScrollManager.Direction.UP);
              manager.setInitialOffset(toolbar.getHeight());
          }
      });*/
      Log.v("TEST", "onCreateView()");
      return v;
  
  }
  
  @Override
  public void onViewCreated(View view , Bundle savedInstanceState) {
      super.onViewCreated(view, savedInstanceState);
      Log.v("TEST", "onViewCreated()");
      mLayoutManager = new GridLayoutManager(getActivity(), 3);
      recyclerView.setLayoutManager(mLayoutManager);
      adapter.setOnItemClickListener(this);
      recyclerView.setAdapter(adapter);
  
  }
  
  @Override public void onItemClick(View view, ViewModel viewModel) {
      // Lire le son ici
      Integer sound = getResources().getIdentifier(viewModel.getSound(), "raw", getActivity().getPackageName());
      playSound(sound);
  }
  
  private void playSound(int resId) {
      if(mPlayer != null) {
          mPlayer.stop();
          mPlayer.release();
      }
      mPlayer = MediaPlayer.create(getActivity(), resId);
      mPlayer.start();
  }
  
  @Override
  public void onPause() {
      super.onPause();
      if(mPlayer != null) {
          mPlayer.stop();
          mPlayer.release();
      }
  }
  
  }
  </code></pre>
  
  <p>I've also tried to put the initialisation of my <code>RecyclerView</code> inside of <code>onCreateView</code> and <code>onViewCreated</code>, with no success.</p>
  
  <p>Any idea?</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>Here is the code of my <code>Adapter</code> :</p>
  
  <pre><code>package com.yomansk8.birthdaysoundboard.Adapters;
  
  import android.os.Handler;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import com.squareup.picasso.Picasso;
  import com.yomansk8.birthdaysoundboard.R;
  import com.yomansk8.birthdaysoundboard.Models.ViewModel;
  
  import java.util.List;
  
  /**
   * Created by Yohann on 19/02/2015.
   */
  public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt; implements View.OnClickListener {
  
  private List&lt;ViewModel&gt; items;
  private OnItemClickListener onItemClickListener;
  
  public RecyclerViewAdapter(List&lt;ViewModel&gt; items) {
  
      this.items = items;
  }
  
  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
      this.onItemClickListener = onItemClickListener;
  }
  
  @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
      v.setOnClickListener(this);
      return new ViewHolder(v);
  }
  
  @Override public void onBindViewHolder(ViewHolder holder, int position) {
      ViewModel item = items.get(position);
      holder.text.setText(item.getText());
      holder.image.setImageBitmap(null);
      Integer mImage = holder.image.getResources().getIdentifier(item.getImage(), "drawable", holder.image.getContext().getPackageName());
      Picasso.with(holder.image.getContext()).load(mImage).into(holder.image);
      holder.itemView.setTag(item);
  }
  
  @Override public int getItemCount() {
      return items.size();
  }
  
  @Override public void onClick(final View v) {
      // Give some time to the ripple to finish the effect
      if (onItemClickListener != null) {
          new Handler().postDelayed(new Runnable() {
              @Override public void run() {
                  onItemClickListener.onItemClick(v, (ViewModel) v.getTag());
              }
          }, 200);
      }
  }
  
  protected static class ViewHolder extends RecyclerView.ViewHolder {
      public ImageView image;
      public TextView text;
  
      public ViewHolder(View itemView) {
          super(itemView);
          image = (ImageView) itemView.findViewById(R.id.image);
          text = (TextView) itemView.findViewById(R.id.text);
      }
  }
  
  public interface OnItemClickListener {
  
      void onItemClick(View view, ViewModel viewModel);
  
  }
  }
  </code></pre>