1

This method is called at button click because of assigned name to the "OnClick property" of the button,what should be the correct code of case in below coding.it giving error that it should be constant expression in case.

public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
  RadioButton AButton = (RadioButton) findViewById(R.id.radio0);
  RadioButton BButton = (RadioButton) findViewById(R.id.radio1);
  if (text.getText().length() == 0) {
    Toast.makeText(this, "Please enter a valid number",
        Toast.LENGTH_LONG).show();
    return;
  }
Sunishtha Singh
  • 321
  • 4
  • 17

2 Answers2

1

You must be using this code in a project that will be marked as library. Is It?

If yes, then you must need to understand that in this case view.getId() value can not be used as constant. In such projects, you can use if else statements.

The reason behind this is, in the main project if you would have defined a resource with the same id, there would be wrong result returned or initialized. So, this is to avoid that situation as it would be more difficult to debug.

Use, if - else statement.

Also, I dont remember the ADT version, but after that view ids are not considered as the constants.

akkilis
  • 1,904
  • 14
  • 21
0

Just convert your switch case statement to if statement and it should suffice. For explanation read switch case statement error: case expressions must be constant expression

Community
  • 1
  • 1
Taras Leskiv
  • 1,835
  • 15
  • 33