Words in quotation marks ("apple", "bread", "cheese") are underlined by a red wavy line! How to fix?

public void switchButton(Button button) { switch (button.getText()) { case "apple": //слова Π² ΠΊΠ°Π²Ρ‹Ρ‡ΠΊΠ°Ρ… ("apple", "bread", "cheese") ΠΏΠΎΠ΄Ρ‡Π΅Ρ€ΠΊΠ½ΡƒΡ‚Π° красной волнистой Π»ΠΈΠ½ΠΈΠ΅ΠΉ!!! Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ? button.setBackground(getResources().getDrawable(R.drawable.apple)); break; case "bread": button.setBackground(getResources().getDrawable(R.drawable.bread)); break; case "cheese": button.setBackground(getResources().getDrawable(R.drawable.cheese)); break; } } 

And used

 Button button1, button2, button3; button1 = (Button)findViewById(R.id.button1); button2 = (Button)findViewById(R.id.button2); button3 = (Button)findViewById(R.id.button3); switchButton(button1); switchButton(button2); switchButton(button3); 
  • @FullyRetarded but I need to check the received text, but I cannot implement it - Developer Chingis
  • And where do you get the text here? Why do I need to rename the buttons, and then get their names? - FullyRetarded
  • There are three buttons and one of the three text ("apple" "bread" "cheese") will randomly fall on them. Text which, respectively, the background will be so. For example, any button got the text of an apple, automatically the background would be R.drawable.apple. Could you convey? - Developer Chingis
  • @DeveloperChingis, first, as @katso replied, this is switch (button.getText()) { replace with this switch (button.getText().toString()) { . secondly, here is the string android.util.Log.v("", "string=" + button.getText().toString()); insert before switch (button.getText().toString()) { . Now open the logs and find string=... tell me what is written. - nick
  • @ L'Esperanza Why if the text from the database image is not put? - Developer Chingis

2 answers 2

button.getText() returns a CharSequence

You need to do this:

 button.getText().toString() 
  • As I understand it, if the text from the database is taken, it does not think so? Just thinks that the text is empty - Developer Chingis

If it underlines with red, then most likely you are using JDK 1.6 or lower. String in the switch construct is available only with java 7. Download JDK 7