安卓:IllegalStateException异常("你不能设置对话框的OnCancelListener或OnDismissListener") [英] Android: IllegalStateException( "You can not set Dialog's OnCancelListener or OnDismissListener")

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

问题描述

这DialogFragment实施导致

This DialogFragment implementation causes an

IllegalStateException异常(你不能设置对话框的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 如下:

public class MainActivity extends Activity implements OkCancelDialogListener{

static final String TAG ="MainActivity";

@Override
public void onCancel(DialogInterface dialog) {


}
}

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

推荐答案

从Android电子文档:

From Android documentation:

公开对话onCreateDialog(包savedInstanceState)

public Dialog onCreateDialog (Bundle savedInstanceState)

覆盖建立你自己的自定义对话框的容器。这是   通常用来显示一个AlertDialog而不是通用对话;   这样做的时候,onCreateView(LayoutInflater,ViewGroup中,包)不   不需要被实施,因为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(包)后调用之前   onCreateView(LayoutInflater,ViewGroup中,包)。默认   实现简单的实例,并返回一个对话框类。

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异常("你不能设置对话框的OnCancelListener或OnDismissListener")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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