我的应用程序不承认 JDK 1.8。我正在尝试使用带有字符串的开关盒作为开关。仅使用 JavaDocs 中的基本示例是的,我可以切换到 if/else 语句,但我不愿意。
public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
return typeOfDay;
}
我收到了不兼容类型:字节、字符、整数、短
的错误,这意味着我正在使用旧版本的Java..这没有意义,因为我有安装了jdk1.8.0_11。
这会是我的 Gradle 文件中的问题吗?
apply plugin: 'com.android.application'
dependencies {
compile 'com.parse.bolts:bolts-android:1.1.2'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
compile 'com.android.support:support-v4:18.0.0'
}
android {
compileSdkVersion 15
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "name"
minSdkVersion 15
targetSdkVersion 15
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
最佳答案
使用this解决方案,我能够摆脱错误。将 targetCompatibility 添加到 Java 1.7 来解决此问题。 Android 当前不在其项目中使用 Java 1.8。
将其添加到 build.gradle 文件中:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
关于java - 无效的字符串 Switch Case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27007504/