35

此 DialogFragment 实现会导致

IllegalStateException("你不能设置 Dialog 的 OnCancelListener 或 OnDismissListener")

. 为什么?解决方案?

public class OkCThreadDialog1 extends DialogFragment{

DialogInterface.OnCancelListener onCancelListener;

public OkCThreadDialog1(){
}

public static OkCThreadDialog1 newInstance(String title, String message) {
    OkCThreadDialog1 frag = new OkCThreadDialog1();
    Bundle args = new Bundle();
    args.putString("title", title);
    args.putString("message", message);
    frag.setArguments(args);
    return frag;
}


public Dialog onCreateDialog(Bundle savedInstanceState){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());


    builder .setTitle(getArguments().getString("title"))
            .setMessage(getArguments().getString("message"))
            .setOnCancelListener(onCancelListener)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }})
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    getDialog().cancel();
                }});

    return builder.create();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        onCancelListener = (DialogInterface.OnCancelListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement OkCancelDialogListener");
    }
}
}

我的活动实现DialogInterface.OnCancelListener如下:

public class MainActivity extends Activity implements OkCancelDialogListener{

static final String TAG ="MainActivity";

@Override
public void onCancel(DialogInterface dialog) {


}
}

从 抛出异常builder.create();。怎么了?

4

4 回答 4

87

来自Android 文档

public Dialog onCreateDialog (Bundle savedInstanceState)

覆盖以构建您自己的自定义对话框容器。这通常用于显示 AlertDialog 而不是通用 Dialog;这样做时,不需要实现 onCreateView(LayoutInflater, ViewGroup, Bundle),因为 AlertDialog 会处理自己的内容。

此方法将在 onCreate(Bundle) 之后和 onCreateView(LayoutInflater, ViewGroup, Bundle) 之前调用。默认实现只是实例化并返回一个 Dialog 类。

注意:DialogFragment 拥有 Dialog.setOnCancelListener 和 Dialog.setOnDismissListener 回调。你不能自己设置它们。

要了解这些事件,请覆盖 onCancel(DialogInterface) 和 onDismiss(DialogInterface)。

所以基本上,你必须覆盖 onDismiss 或 OnCancel 而不是 '.setOnCancelListener(onCancelListener)'。

于 2013-02-14T13:46:57.383 回答
6

你可以试试这个:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Title");
    builder.setMessage("Message");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // do something positive
        }
    });
    return builder.create();
}

@Override
public void onCancel(DialogInterface dialog) {
    // do something
}

希望它可以帮助...

于 2015-07-20T14:07:36.620 回答
0

您为对话框设置 OnCancelListener,而不是为其构建器设置。

public Dialog onCreateDialog(Bundle savedInstanceState){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder .setTitle(getArguments().getString("title"))
        .setMessage(getArguments().getString("message"))
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }})
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                getDialog().cancel();
            }});

    Dialog dialog = builder.create();
    dialog.setOnCancelListener(onCancelListener);
    return dialog;
}
于 2013-01-25T11:16:02.093 回答
-3

试试这个代码,我希望这会起作用..

AlertDialog.Builder builder = new AlertDialog.Builder(
            class_name.this);
    builder.setTitle("Title");      
    AlertDialog dialog = builder.create();
    dialog.setButton("ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {            

    });


    dialog.setButton2("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {


            dialog.cancel();

        }
    });
    dialog.show();
于 2013-01-25T13:25:35.607 回答