使用Eclipse Android项目给出错误:无法String类型的值转换为低于1.7源代码级 [英] Android project using Eclipse gives error: Cannot switch on a value of type String for source level below 1.7

查看:564
本文介绍了使用Eclipse Android项目给出错误:无法String类型的值转换为低于1.7源代码级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在Android项目的工作使用eclipse,突然我开始收到此错误:

I was working on an android project using eclipse and suddenly i started to get this error:

无法在低于1.7源代码级的一个String类型的值进行切换。只有敞篷int类型或枚举变量是允许的。

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted.

我试着在previous类似的问题,每一个解决方案,但他们没有工作对我来说...

I tried every solution in previous similar issues but none of them worked for me...

我曾尝试固定项目,并在我的两个项目,并为所有项目JDK遵从级别设置为1.7。

I have Tried fixing the project and setting the JDK compliance level to 1.7 in both my project and for all project.

我使用ADT生成:v22.2.1-833290和Eclipse:

I am using ADT Build: v22.2.1-833290 and Eclipse:

String text = mService.getString();
switch (text) {
    case Protocols.REQUEST_SEND_MESSAGE:
        publishProgress("sent");
        break;
    case Protocols.RESPONSE_OK:
        mService.sendMessage("mesasage");   
        publishProgress("sent");  
        break;              
    default:
        break;
}

这是怎么回事?

推荐答案

您尝试使用开关 / 情况字符串的对象,这是只在Java 1.7或更高版本可用。 Android的ADT需要Java 1.6。这意味着你不能使用开关字符串结构。刚刚与更换它,如果 / 其他

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.

这个替换您的code。

Replace your code with this.

String text=mService.getString();
if (Protocols.REQUEST_SEND_MESSAGE.equals(text)) {
    publishProgress("sent");
} else if (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.

这篇关于使用Eclipse Android项目给出错误:无法String类型的值转换为低于1.7源代码级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆