【问题标题】:IllegalStateException( "You can not set Dialog's OnCancelListener or OnDismissListener")IllegalStateException("你不能设置 Dialog 的 OnCancelListener 或 OnDismissListener")
【发布时间】:2013-01-25 11:11:18
【问题描述】:

此 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) {


}
}

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

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    来自Android documentation

    public Dialog onCreateDialog (Bundle savedInstanceState)

    重写以构建您自己的自定义对话框容器。这是 通常用于显示 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)'。

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      @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】:

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

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

        【讨论】:

        • 这对我也不起作用。我只是在接受的答案中使用了解决方案。
        【解决方案4】:

        试试这个代码,我希望它能工作..

        AlertDialog.Builder builder = new AlertDialog.Builder(
                    class_name.this);
            builder.setTitle("Title");      
            AlertDialog dialog = builder.create();
            dialog.setButton("ok", new DialogInterface.OnClickListener() {
        
                public void onClick(DialogInterface dialog, int which) {            
        
            });
        
        
            dialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
        
                @Override
                public void onClick(DialogInterface dialog, int which) {
        
        
                    dialog.cancel();
        
                }
            });
            dialog.show();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-29
          • 1970-01-01
          相关资源
          最近更新 更多