Search code examples
androidandroid-recyclerviewdialogfragment

ClassCastException on calling a dialogfragment from RecyclerView


I have a RecyclerView from which I am trying to call a dialogfragment but I am getting a class cast exception. I tried a lot of solutions on stack but it didn't help. Please help me.

    java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
12-04 14:08:52.495 6146-    6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.opentok.example.efflorescence.myaudiotranscription.AllCallsRecyclerAdapter.showTranscriptionDialog(AllCallsRecyclerAdapter.java:249)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.opentok.example.efflorescence.myaudiotranscription.AllCallsRecyclerAdapter$1$1.run(AllCallsRecyclerAdapter.java:210)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Handler.handleCallback(Handler.java:815)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:104)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.os.Looper.loop(Looper.java:207)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5765)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-04 14:08:52.495 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
12-04 14:08:52.496 6146-6146/com.opentok.example.efflorescence.myaudiotranscription W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

my adapter code onClick

public void showTranscriptionDialog() {
  try {
    FragmentManager fr = ((Activity) context).getFragmentManager();
    TranscriptionDialogFragment msgDialog = new TranscriptionDialogFragment();
    msgDialog.show(fr, "Dialog");
  }
    catch (Exception e) {
    e.printStackTrace();
    Log.e("error displaying dialog",e.getLocalizedMessage());
  }
}

my fragment

public class TranscriptionDialogFragment extends DialogFragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_transcription, container, false);
    getDialog().setTitle("Simple Dialog");
    return rootView;
  }
}

My Contructor in adapter:

public AllCallsRecyclerAdapter(List<AllCallsData> list, Context context) {
  this.mInflater = LayoutInflater.from(context);
  this.list = list;
  this.context = context;
  mydb = new DBHelper(context);
}

Solution

  • This is because you're trying to cast the Application context to activity in the following code:

    FragmentManager fr = ((Activity) context).getFragmentManager();
    

    which is an error.

    You need to make sure the context is an activity not an application.