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

后端 未结 4 1690
独厮守ぢ
独厮守ぢ 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: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;
    }
    

提交回复
热议问题