Luiz Alves Luiz Alves - 1 year ago 58
Android Question

Android Exception calling DialogFragment from Fragment

I have created a dialogfragment I´d like to call it from a Fragment.

I am getting an exception about MainActivity should implement the Listener.

But I implemented it into Fragment. What is wrong?

E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.techabout.appointment, PID: 4162
java.lang.ClassCastException: net.techabout.appointment.MainActivity@3a11f41 deve implementar MyDialogFragmentListener
at net.techabout.appointment.AndroidDialogFragment.onAttach(

/////My Fragment

public class HorariosMedFragment extends Fragment implements
AndroidDialogFragment.MyDialogFragmentListener {

void OpenDialog(String title, String msg, String but_sim, String but_nao) {
AndroidDialogFragment myDialogFragment = AndroidDialogFragment
.newInstance(title, msg, but_sim, but_nao);, "myDialogFragment");
public void onDialogPositiveClick(DialogFragment dialog) {
//do something
public void onDialogNegativeClick(DialogFragment dialog) {



public class AndroidDialogFragment extends DialogFragment {

public interface MyDialogFragmentListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);

// Usa essa instância da interface para entregar eventos de ação
MyDialogFragmentListener mListener;

static AndroidDialogFragment newInstance(String titulo, String msg,String but_sim, String but_nao) {
AndroidDialogFragment dialog = new AndroidDialogFragment();
Bundle args = new Bundle();
args.putString("title", titulo);
args.putString("but_sim", but_sim);
args.putString("but_nao", but_nao);
args.putString("msg", msg);

return dialog;

public void onAttach(Context context) {
// Verifica se a activity implementa a interface de callbacks
try {
// Instancia o MyDialogFragmentListener para que possamos enviar
// eventos para o host
mListener = (MyDialogFragmentListener) context;
} catch (ClassCastException e) {
// Essa activity não implementa a interface, levanta exceção
throw new ClassCastException(context.toString()
+ " deve implementar MyDialogFragmentListener");

public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String but_sim = getArguments().getString("but_sim");
String but_nao = getArguments().getString("but_nao");
String msg = getArguments().getString("msg");
Dialog myDialog = new AlertDialog.Builder(getActivity())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// Dispara o evento onDialogPositiveClick para a
// activity que estiver escultando
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
// Dispara o evento onDialogNegativeClick para a
// activity que estiver escultando

return myDialog;


Answer Source

While creating Dialog Fragment instance set target fragment like


After that in DialogFragment's onCreate method get that targeted fragment

public void onCreate(Bundle savedInstanceState) {

    try {
         mListener = (MyDialogFragmentListener) getTargetFragment();

    } catch (ClassCastException e) {
        throw new ClassCastException("Calling Fragment must implement MyDialogFragmentListener");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download