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

查看:365
本文介绍了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");
    }
}
}

我的活动实现了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();引发欺骗.怎么了?

推荐答案

来自 Android文档:

onDialog上的公共对话框(已保存的BundleInstanceState)

public Dialog onCreateDialog (Bundle savedInstanceState)

重写以构建自己的自定义Dialog容器.这是 通常用于显示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).

To find out about these events, override onCancel(DialogInterface) and 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天全站免登陆