Android Project Using Eclipse Gives Error: Cannot Switch On A Value Of Type String For Source Level Below 1.7
I was working on an android project using eclipse and suddenly i started to get this error: Cannot switch on a value of type String for source level below 1.7. Only convertible in
Solution 1:
You are trying to use switch
/ case
with String
objects, which is only available in Java 1.7 or higher. Android ADT requires Java 1.6. This means you cannot use switch
with String
construct. Just replace it with if
/ else
.
Replace your code with this.
String text=mService.getString();
if (Protocols.REQUEST_SEND_MESSAGE.equals(text)) {
publishProgress("sent");
} elseif (Protocols.RESPONSE_OK.equals(text)) {
mService.sendMessage("mesasage");
publishProgress("sent");
}
Another option would be to create an enum
and put all Protocol constants into there. Then you will be able to use switch
/ case
with enum
values.
Solution 2:
As the answer below gives more details, switch statement on String objects is a new feature introduced in Java 1.7. Unfortunatelly Android requires version 1.6 or 1.5. :
Post a Comment for "Android Project Using Eclipse Gives Error: Cannot Switch On A Value Of Type String For Source Level Below 1.7"