IllegalStateException( “You can not set Dialog's OnCancelListener or OnDismissListener”)

后端 未结 4 1674
独厮守ぢ
独厮守ぢ 2020-12-16 09:41

This DialogFragment implementation causes an

IllegalStateException( \"You can not set Dialog\'s OnCancelListener or OnDismissListener\")

相关标签:
4条回答
  • 2020-12-16 10:00

    Try this code i hope this will be work..

    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();
    
    0 讨论(0)
  • 2020-12-16 10:05

    You may try this:

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

    Hope it can help ...

    0 讨论(0)
  • 2020-12-16 10:12

    From 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).

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

    0 讨论(0)
  • 2020-12-16 10:19

    You set the OnCancelListener for a dialog not for its builder.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题