Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

java - Android coding with switch (String)

I'm developing an android app on eclipse, when I try some code like

switch(String){
 ......
}

it give me the following error

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

so I changed the compiler to 1.7, then it give me another error

Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.

So, is there anyway to let switch(String) works in android application? or I have no choice but to use

if(){}
else if(){}
else if(){}
........
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the Release of API 19 KitKat you can now do this !!!! This is the change you need for android studio for existing projects. ensure that you are using JDK 1.7 or later and Gradle plugin 0.6.1 or later

android {
compileSdkVersion 19
buildToolsVersion "19.0.0"

defaultConfig {
    minSdkVersion 7
    targetSdkVersion 19
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
} }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...