gpt4 book ai didi

java - 使用 Eclipse 的 Android 项目给出错误 : Cannot switch on a value of type String for source level below 1. 7

转载 作者:行者123 更新时间:2023-11-29 07:50:38 24 4
gpt4 key购买 nike

我正在使用 eclipse 开发一个 android 项目,突然我开始遇到这个错误:

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

我尝试了以前类似问题中的所有解决方案,但没有一个对我有用...

我已尝试在我的项目和所有项目中修复项目并将 JDK 合规级别设置为 1.7。

我正在使用 ADT 构建:v22.2.1-833290 和 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;
}

这是怎么回事?

最佳答案

您正在尝试将 switch/caseString 对象一起使用,这仅在 Java 1.7 或更高版本中可用。 Android ADT 需要 Java 1.6。这意味着您不能将 switchString 构造一起使用。只需将其替换为 if/else

用这个替换你的代码。

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");
}

另一种选择是创建一个 enum 并将所有协议(protocol)常量放入其中。然后您将能够使用 switch/caseenum 值。

关于java - 使用 Eclipse 的 Android 项目给出错误 : Cannot switch on a value of type String for source level below 1. 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com