title: Recycler and CardView : invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
body: <p>I have a cardView adapter which allows me to display data from my database. I have just implemented a swipe to delete but sometimes I have this error:</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
  </code></pre>
  
  <p>that points to this line of code <code>mRecyclerView.removeViewAt (viewHolder.getAdapterPosition ());</code> in my Activity</p>
  
  <p>I do not understand why I have this error especially that sometimes it works very well but sometimes not ...</p>
  
  <p><strong>My activity</strong></p>
  
  <pre><code>public class Resume_game_new extends AppCompatActivity {
  
      private RecyclerView mRecyclerView;
      private RecyclerView.Adapter mAdapter;
      private RecyclerView.LayoutManager mLayoutManager;
      List&lt;Game&gt; mDataset;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_resume_game_new);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_resume_game);
  
          // use this setting to improve performance if you know that changes
          // in content do not change the layout size of the RecyclerView
          mRecyclerView.setHasFixedSize(true);
  
          // use a linear layout manager
          mLayoutManager = new LinearLayoutManager(this);
          mRecyclerView.setLayoutManager(mLayoutManager);
  
          // specify an adapter (see also next example)
          mDataset = Data.bdd.getGames();
          mAdapter = new CardsViewAdapter(mDataset);
          mRecyclerView.setAdapter(mAdapter);
  
          //Swipe to Delete
          ItemTouchHelper swipeToDismissTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
                  ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
              @Override
              public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                  return false;
              }
  
              @Override
              public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction)
              {
  
                  // Do Stuff
                  TextView textView_id_game = (TextView) viewHolder.itemView.findViewById(R.id.textView16);
                  Data.bdd.remove_game(Integer.parseInt(textView_id_game.getText().toString()));
                  mDataset.remove(viewHolder.getAdapterPosition());
  
  
                  mRecyclerView.removeViewAt(viewHolder.getAdapterPosition());
                  mAdapter.notifyItemRemoved(viewHolder.getLayoutPosition());
                  mAdapter.notifyItemRangeChanged(viewHolder.getAdapterPosition(), mDataset.size());
                  mAdapter.notifyDataSetChanged();
  
              }
  
          });
          swipeToDismissTouchHelper.attachToRecyclerView(mRecyclerView);
      }
  
  }
  </code></pre>
  
  <p><strong>My adapter</strong> (only revelant code)</p>
  
  <pre><code>public class CardsViewAdapter extends RecyclerView.Adapter&lt;CardsViewAdapter.ViewHolder&gt; {
      private List&lt;Game&gt; mDataset;
      private int rotationAngle = 0;
  
      // 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
      static class ViewHolder extends RecyclerView.ViewHolder {
          // each data item is just a string in this case
          private TextView textView_idGame;
          private ImageView imageView_arrow;
          private LinearLayout linearlayoutToExpands;
          private ConstraintLayout coordinatorLayoutToExpands;
          boolean isPopupVisible;
          private CardView cardView;
  
  
          ViewHolder(View v) {
              super(v);
              textView_idGame = (TextView) v.findViewById(R.id.textView16);
              imageView_arrow = (ImageView) v.findViewById(R.id.item_description_game_more);
              linearlayoutToExpands = (LinearLayout) v.findViewById(R.id.popup_layout);
              isPopupVisible = false;
              cardView = (CardView) v.findViewById(R.id.cardView);
  
  
              coordinatorLayoutToExpands = (ConstraintLayout) v.findViewById(R.id.viewToExpands);
          }
      }
  
      // Provide a suitable constructor (depends on the kind of dataset)
      CardsViewAdapter(List&lt;Game&gt; myDataset) {
          mDataset = myDataset;
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public CardsViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
          // create a new view
          View v = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.cards_resume_game, parent, false);
          // set the view's size, margins, paddings and layout parameters
          //...
  
          ViewHolder vh = new ViewHolder(v);
          return vh;
      }
  
      // Replace the contents of a view (invoked by the layout manager)
      @Override
      public void onBindViewHolder(final ViewHolder holder, final int position) {
          // - get element from your dataset at this position
          // - replace the contents of the view with that element
          final Game game = mDataset.get(position);
  
  ...
  
          // Set the expanded or collapsed mode here
          if(game.expanded) {
              expandView(holder.coordinatorLayoutToExpands);
              holder.imageView_arrow.setRotation(180);
          }
          else {
              collapseView(holder.coordinatorLayoutToExpands);
              holder.imageView_arrow.setRotation(0);
          }
  
  
          // Now set the onClickListener like this
          holder.imageView_arrow.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  // Animate the imageView here
                  animateImageView(holder.imageView_arrow);
  
                  // Toggle the expanded attribute value
                  if(game.expanded) game.expanded = false;
                  else game.expanded = true;
  
                   //Now call notifyDataSetChanged to make the change to effect
                  refreshList(position);
              }
          });
      }
  
      // Extra functions inside your adapter class to improve readability
      private void collapseView(View v) {
          CardsAnimationHelper.collapse(v);
      }
  
      private void expandView(View v) {
          CardsAnimationHelper.expand(v);
      }
  
      private void refreshList(final int position) {
          final Handler handler = new Handler();
          handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  notifyItemChanged(position);
              }
          }, animationDuration);
      }
  
      // Return the size of your dataset (invoked by the layout manager)
      @Override
      public int getItemCount() {
          return mDataset.size();
      }
  
  
  }
  </code></pre>