title: RecyclerView with Fragments
body: <p>I am trying to use the new <code>RecyclerView</code> widget inside a <code>Fragment</code> but I am getting this error: </p>
  
  <blockquote>
    <p>Unable to start activity
    ComponentInfo{com.example.myapplication/com.example.myapplication.MyActivity}:
    java.lang.NullPointerException: Attempt to invoke interface method
    'boolean java.util.List.add(java.lang.Object)' on a null object
    reference</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>This is my <code>Fragment</code>:</p>
  
  <pre><code>import android.app.Fragment;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v7.widget.DefaultItemAnimator;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import java.util.List;
  
  public class RecyclerviewFragment extends Fragment {
  
      private RecyclerView mRecyclerView;
      private RecyclerviewAdapter mRecyclerviewAdapter;
      private LinearLayoutManager mLinearLayoutManager;
  
      private List&lt;ViewModel&gt; viewModel;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
          mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
  
          return rootView;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          mRecyclerviewAdapter = new RecyclerviewAdapter(viewModel);
          mLinearLayoutManager = new LinearLayoutManager(getActivity());
          mRecyclerView.setAdapter(mRecyclerviewAdapter);
          mRecyclerView.setLayoutManager(mLinearLayoutManager);
          mRecyclerView.setItemAnimator(new DefaultItemAnimator());
      }
  }
  </code></pre>
  
  <p>And my Activity:</p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  import java.util.List;
  
  public class MyActivity extends Activity {
  
      private List&lt;ViewModel&gt; viewModel;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_my);
  
          RecyclerviewFragment recyclerviewFragment = new RecyclerviewFragment();
          getFragmentManager().beginTransaction().add(android.R.id.content, recyclerviewFragment).commit();
  
          viewModel.add(new ViewModel("View"));
  
  
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.my, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>Thanks!</p>