title: Custom Alert Dialog With RecyclerView
body: <p>I'm using <code>RecyclerView</code> to list some text and now I want to make it so that when the user clicks on text a custom Alert Dialog box pops up.</p>
  
  <p>I have tried this so far but get a NullPointerException; what could be wrong here?</p>
  
  <pre><code>public class CBAdapter extends RecyclerView.Adapter&lt;CBAdapter.ViewHolder&gt; {
  
  List&lt;AdapterData&gt; mItems;
  
  public CBAdapter() {
      super();
      mItems = new ArrayList&lt;&gt;();
      AdapterData data = new AdapterData();
      data.setTextOne("Many Bows");
      mItems.add(data);
  
      data = new AdapterData();
      data.setTextOne("Pardon");
      mItems.add(data);
  
      data = new AdapterData();
      data.setTextOne("Fall To Knees &amp; Beg");
      mItems.add(data);
  
      data = new AdapterData();
      data.setTextOne("Backflips");
      mItems.add(data);
  
  
  
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
      View v = LayoutInflater.from(viewGroup.getContext())
              .inflate(R.layout.test3, viewGroup, false);
      return new ViewHolder(v);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder viewHolder, int i) {
      AdapterData data = mItems.get(i);
      viewHolder.textOne.setText(data.getTextOne());
  
  }
  
  
  @Override
  public int getItemCount() {
  
      return mItems.size();
  }
  
  class ViewHolder extends RecyclerView.ViewHolder{
  
      public TextView textOne;
      private Context context;
  
  
  
  
  
      public ViewHolder(View itemView) {
          super(itemView);
          textOne = (TextView)itemView.findViewById(R.id.textView1);
  
          itemView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  final Dialog dialog = new Dialog(context);
                  dialog.setContentView(R.layout.custom_dialog);
                  dialog.setTitle("Title");
  
                  TextView text = (TextView) dialog.findViewById(R.id.text);
                  text.setText("hello world");
  
                  ImageView image = (ImageView) dialog.findViewById(R.id.image);
                  image.setImageResource(R.drawable.ic_launcher);
  
                  Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
  
                  dialogButton.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          dialog.dismiss();
                      }
                  });
  
                  dialog.show();
              }
          });
  
  
      }
  }
  }
  </code></pre>