have this code i only need show one toast if click accept or cancel,but cant understant why is wrong how can fix? error is on toast getapplicationcontext() im learn about dialogs and toast on android

    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.util.Log;
    import android.widget.Toast;

    public class confirmacion extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
        builder.setMessage("Confirmar?").setTitle("confirmacion")
        .setPositiveButton("aceptar", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
    Log.i("dialogos","aceptado");
    dialog.cancel();
    Toast.makeText(getApplicationContext(), "Eliminacion fallida.", Toast.LENGTH_LONG).show();

            }


        }).setNegativeButton("cancelar", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
    Log.i("dialogos","cancelada");
    dialog.cancel();
            }
        });

        return builder.create();

    }

    }
有帮助吗?

解决方案

Change from

Toast.makeText(getApplicationContext(), "Eliminacion fallida.", Toast.LENGTH_LONG).show();

to

Toast.makeText(getActivity(), "Eliminacion fallida.", Toast.LENGTH_LONG).show();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top