android– 如何在自定义对话框中添加radiogroup onchecked listener

android– 如何在自定义对话框中添加radiogroup onchecked listener,第1张

概述我有一个包含单选按钮的自定义对话框,但是当我添加一个oncheckedchangelistener时,我的应用程序崩溃了.可能是什么问题呢?提前致谢!:d这是我的代码:GridViewAdapteradapter=newGridViewAdapter(alcoholType.this,images,names);gridView.setAdapter(adapter);gridV

我有一个包含单选按钮的自定义对话框,但是当我添加一个oncheckedchangelistener时,我的应用程序崩溃了.可能是什么问题呢?

提前致谢! :d

这是我的代码:

GrIDVIEwAdapter adapter = new GrIDVIEwAdapter(alcoholType.this, images,names);    grIDVIEw.setAdapter(adapter);    grIDVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        @OverrIDe        public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw,                                int position, long ID) {            final Dialog dialog = new Dialog(context);            dialog.setContentVIEw(R.layout.alcoholdialog);            dialog.setTitle("ORDER " + names.get(position));            radioQuantity = (RadioGroup) findVIEwByID(R.ID.radioQuantity);            quantity1 = (Radiobutton) findVIEwByID(R.ID.quantity1);            quantity2 = (Radiobutton) findVIEwByID(R.ID.quantity2);            radioQuantity.clearCheck();            radioQuantity.setonCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {                        @OverrIDe                        public voID onCheckedChanged(RadioGroup group, int checkedID) {                        }                    });            dialog.show();        }    });

这是错误跟踪:

E/AndroIDRuntime: FATAL EXCEPTION: main              Process: com.example.gin.ordering, PID: 22005              java.lang.NullPointerException: Attempt to invoke virtual method 'voID androID.Widget.RadioGroup.clearCheck()' on a null object reference                  at com.example.gin.ordering.alcoholType.onItemClick(alcoholType.java:90)                  at androID.Widget.AdapterVIEw.performItemClick(AdapterVIEw.java:339)                  at androID.Widget.AbsListVIEw.performItemClick(AbsListVIEw.java:1549)                  at androID.Widget.AbsListVIEw$PerformClick.run(AbsListVIEw.java:3723)                  at androID.Widget.AbsListVIEw.run(AbsListVIEw.java:5707)                  at androID.os.Handler.handleCallback(Handler.java:739)                  at androID.os.Handler.dispatchMessage(Handler.java:95)                  at androID.os.Looper.loop(Looper.java:145)                  at androID.app.ActivityThread.main(ActivityThread.java:6918)                  at java.lang.reflect.Method.invoke(Native Method)                  at java.lang.reflect.Method.invoke(Method.java:372)                  at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)                  at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

解决方法:

您需要使用Dialog的引用来获取视图:

使用:

radioQuantity = (RadioGroup) dialog.findVIEwByID(R.ID.radioQuantity);quantity1 = (Radiobutton) dialog.findVIEwByID(R.ID.quantity1);quantity2 = (Radiobutton) dialog.findVIEwByID(R.ID.quantity2);

您得到NullPointerException,因为您的RadioGroup未附加到视图.

另外对于CheckedListener使用:

radioQuantity.setonCheckedchangelistener(new    RadioGroup.OnCheckedchangelistener() {        @OverrIDe        public voID onCheckedChanged(RadioGroup group, int checkedID) {            if (checkedID == R.ID.quantity1) {                //Your code            } else if (checkedID == R.ID.quantity2) {                //Your code            }         }    });
总结

以上是内存溢出为你收集整理的android – 如何在自定义对话框中添加radiogroup onchecked listener全部内容,希望文章能够帮你解决android – 如何在自定义对话框中添加radiogroup onchecked listener所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存