IllegalStateException( "你不能设置 Dialog 的 OnCancelListener 或 OnDismissListener") [英] IllegalStateException( "You can not set Dialog's OnCancelListener or OnDismissListener")

查看:33
本文介绍了IllegalStateException( "你不能设置 Dialog 的 OnCancelListener 或 OnDismissListener")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个 DialogFragment 实现导致一个

This DialogFragment implementation causes an

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

IllegalStateException( "You can not set Dialog's OnCancelListener or 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如下:

My Activity implements DialogInterface.OnCancelListener as follow:

public class MainActivity extends Activity implements OkCancelDialogListener{

static final String TAG ="MainActivity";

@Override
public void onCancel(DialogInterface dialog) {


}
}

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

Exeception is thrown from builder.create();. What's wrong?

推荐答案

来自 Android 文档:

公共对话框 onCreateDialog (Bundle savedInstanceState)

public Dialog onCreateDialog (Bundle savedInstanceState)

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

Override to build your own custom Dialog container. This is typically used to show an AlertDialog instead of a generic Dialog; when doing so, onCreateView(LayoutInflater, ViewGroup, Bundle) does not need to be implemented since the AlertDialog takes care of its own content.

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

This method will be called after onCreate(Bundle) and before onCreateView(LayoutInflater, ViewGroup, Bundle). The default implementation simply instantiates and returns a Dialog class.

注意:DialogFragment 拥有 Dialog.setOnCancelListener 和 Dialog.setOnDismissListener 回调.您不得自行设置.

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

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

So basically, you must override the onDismiss or OnCancel instead of '.setOnCancelListener(onCancelListener)'.

这篇关于IllegalStateException( "你不能设置 Dialog 的 OnCancelListener 或 OnDismissListener")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆