title: Callback from an adapter to a function inside fragment
body: <p>My adapter callback is not working.</p>
  <p>I want to activate an alert dialog to pop up after the user clicks on one of the cards in recycle view. To implement the same I followed <a href="https://stackoverflow.com/a/27716788/10792358">this</a> answer. During runtime, when I click on any of the cards, my application crashes and refreshes back to the home page of my application.</p>
  <p>This is the error output of the LogCat.<br />
  First half of Logcat.
  <a href="https://i.stack.imgur.com/yp7ZD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yp7ZD.png" alt="logcat1" /></a></p>
  <p>Second half of Logcat:</p>
  <p><a href="https://i.stack.imgur.com/eK48T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eK48T.png" alt="logcat2" /></a></p>
  <p>My fragment file. This fragment is part of one of three fragments inside the tabbed activity.</p>
  <p><strong>SpecializationFrag.java</strong></p>
  <pre><code>public class SpecializationFrag extends Fragment implements AdapterCallback{
  
      // This is minimum reproducible code. 
      // other code which I found unnecessary are skipped
      
      @Override
      public void onMethodCallback( ) {
          Toast.makeText(getContext(), &quot;Inside fragment, methodcallback &quot;, Toast.LENGTH_SHORT).show();
  
      }
  }
  </code></pre>
  <p>My adapter file.<br />
  <strong>SpecializationAdapter.java</strong></p>
  <pre><code>public class SpecializationAdapter extends RecyclerView.Adapter&lt;SpecializationAdapter.MyViewHolder&gt; {
      ArrayList&lt;DoctorsList&gt; doctorsListArrayList;
      private AdapterCallback adapterCallback;
      
      // some more code that I've skipped
      
      public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  
          public SpecializationAdapter(Context context, ArrayList&lt;DoctorsList&gt; doctorsListArrayList){ // constructor for initialization
              this.context = context;
              this.doctorsListArrayList = doctorsListArrayList;
              try{
                  adapterCallback = ((AdapterCallback) context);
              }catch (ClassCastException e) {
                  Log.d(&quot;Error&quot;, e.toString());
              }
          }
  
          @Override
          public void onBindViewHolder(@NonNull com.example.appointmentmodule.Specialization.SpecializationAdapter.MyViewHolder holder, int position) {
              
              holder.Specialization.setText(doctorsList.getSpecialization());
  
              holder.itemView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      try{
                          adapterCallback.onMethodCallback();
  
                      }catch (ClassCastException e){
                          Log.d(&quot;Error&quot;, e.toString());
                      }
                  }
              });
              
          }
      }
  
      }  
  </code></pre>
  <p>adapterCallback file.</p>
  <p><strong>AdapterCallback.java</strong></p>
  <pre><code>public interface AdapterCallback {
  
  void onMethodCallback();
  }
  </code></pre>