android–IllegalStateException(“你不能设置Dialog的OnCancelListener或OnDismissListener”)

android–IllegalStateException(“你不能设置Dialog的OnCancelListener或OnDismissListener”),第1张

概述这个DialogFragment实现导致了IllegalStateException(“YoucannotsetDialog’sOnCancelListenerorOnDismissListener”).为什么?解?publicclassOkCThreadDialog1extendsDialogFragment{DialogInterface.OnCancelListeneronCancelListener;publicOkCThrea

这个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();}@OverrIDepublic 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";@OverrIDepublic voID onCancel(DialogInterface dialog) {}}

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

解决方法:

来自AndroID文档:

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(“你不能设置Dialog的OnCancelListener或OnDismissListener”)全部内容,希望文章能够帮你解决android – IllegalStateException(“你不能设置Dialog的OnCancelListener或OnDismissListener”)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1100247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存