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

问题描述 投票:35回答:4

这个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");
    }
}
}

我的Activity实现了DialogInterface.OnCancelListener如下:

public class MainActivity extends Activity implements OkCancelDialogListener{

static final String TAG ="MainActivity";

@Override
public void onCancel(DialogInterface dialog) {


}
}

qazxsw poi抛出异常。怎么了?

android android-dialogfragment
4个回答
87
投票

来自Android文档:

public Dialog onCreateDialog(Bundle savedInstanceState)

覆盖以构建自己的自定义Dialog容器。这通常用于显示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)'。


6
投票

你可以试试这个:

builder.create();

希望它可以帮助......


0
投票

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

@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
}

-3
投票

试试这个代码我希望这会有效..

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.