0

(https://i.stack.imgur.com/HydEx.png)

hey guys, i have problem with switch case in android studio in this case i have a radio group "radioGroup_newAc_n1" and 3 radio button "radioButton_newAc_rb1,rb2,rb3" and i wrote this code, but i got this error in the compilation process... whats wrong? you can see deatails in pic...

        RadioGroup rgOptions = findViewById(R.id.radioGroup_newAc_n1);
        rgOptions.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radioButton_newAc_rb1: // <--Line72
                        Toast.makeText(NewAc.this,"rb1 is checked",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.radioButton_newAc_rb2: // <--Line75
                        Toast.makeText(NewAc.this,"rb2 is checked",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.radioButton_newAc_rb3: // <--Line78
                        Toast.makeText(NewAc.this,"rb3 is checked",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

Error:

NewAc.java:72: error: constant expression required
                    case R.id.radioButton_newAc_rb1:
                             ^
NewAc.java:75: error: constant expression required
                    case R.id.radioButton_newAc_rb2:
                             ^
NewAc.java:78: error: constant expression required
                    case R.id.radioButton_newAc_rb3:
                             ^

this is my first question in stackoverflow and actually don't have idea what's need to be here :D

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Kian
  • 1
  • 1
  • What's unclear about `constant expression required`? – Olivier Aug 12 '23 at 08:16
  • Possibly related: [constant expression required when trying to create a switch case block](https://stackoverflow.com/q/76430646), [what causes "constant expression required" errors for the generated R.id.xxx values in switch statements?](https://stackoverflow.com/q/21005205) – Pshemo Aug 12 '23 at 08:16
  • @Olivier Owe you one ;D – Kian Aug 12 '23 at 08:36

0 Answers0