android - IllegalStateException( "You can not set Dialog' s OnCancelListener 或 OnDismissListener")

标签 android android-dialogfragment

此 DialogFragment 实现会导致

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 如下:

public class MainActivity extends Activity implements OkCancelDialogListener{

static final String TAG ="MainActivity";

@Override
public void onCancel(DialogInterface dialog) {


}
}

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

最佳答案

来自 Android documentation :

public Dialog onCreateDialog (Bundle savedInstanceState)

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.

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

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself.

To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

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

关于android - IllegalStateException( "You can not set Dialog' s OnCancelListener 或 OnDismissListener"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14520597/

相关文章:

java - 从命令行 “invalid resource directory name” 生成 R.java 文件

android - 如果设置了 STYLE_NO_TITLE,带有 AppCompatDialog 的 DialogFragment 会崩溃

java - 实现 DialogFragment 的回调

java - 看不到 DialogFragment 中长 ListView 底部的按钮

java - libgdx 如何检测冲突?

android - 单击外部时如何隐藏布局?

android - fragment 未与 fragment 管理器关联。 (对话 fragment )

android - 持续配置更改的 DialogFragment 监听器

android - 我可以在 Android MediaPlayer 中使用基本 HTTP 身份验证吗?

android - ImageView setMargins 不起作用