如何在自定义对话框中添加RadioButton组的选中监听器?

4
我有一个自定义对话框,其中包含单选按钮,但是当我添加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$1.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$3.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)

发布错误跟踪 - Jonathan Aste
2个回答

5

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

使用:

radioQuantity = (RadioGroup) dialog.findViewById(R.id.radioQuantity);
quantity1 = (RadioButton) dialog.findViewById(R.id.quantity1);
quantity2 = (RadioButton) dialog.findViewById(R.id.quantity2);

如果您的RadioGroup未连接到视图,则会出现NullPointerException

对于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
            } 

        }
    });

1
这是最简单的问题哈哈。我简直不敢相信我错过了那个,谢谢您先生! - Gionne Lapuz

2
尝试使用以下代码:

尝试以下代码

radioQuantity = (RadioGroup) dialog.findViewById(R.id.radioQuantity);
quantity1 = (RadioButton) dialog.findViewById(R.id.quantity1);
quantity2 = (RadioButton) dialog.findViewById(R.id.quantity2);

 radioQuantity.setOnCheckedChangeListener(new 
       RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (checkedId == R.id.quantity1) {
                    Log.d("radiobox", "clickingevent");
                    optionans = "1";
                } else if (checkedId == R.id.quantity2) {

                    optionans = "2";
                } 

            }
        });

1
先生,我已经找到解决方案了,只是我错过了对话框.findViewById(R.id.radioQuantity); 哈哈.. 无论如何,谢谢您的帮助! - Gionne Lapuz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接