带有Toast dialoginterface的Android错误dialogfragment

时间:2014-03-01 11:34:28

标签: android toast dialogfragment

有这个代码我只需要显示一个吐司,如果点击接受或取消,但不能理解为什么是错的 怎么修好?吐司错误getapplicationcontext() 我在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();

    }

    }

1 个答案:

答案 0 :(得分:6)

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

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