In every case it gives me the error case expressions must be constant expressions.I am working on application on Google Maps API V2 and I have added the map types to the Option Menu (onOptionItemSelected)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mapTypeNormal:
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeTerrain:
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeSatellite:
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeHybrid:
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.gotoCurrentLocation:
gotoCurrentLocation();
break;
default:
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
tried converting switch into if-else..But the same error appears
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.mapTypeNormal) {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
} else if (itemId == R.id.mapTypeTerrain) {
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
} else if (itemId == R.id.mapTypeSatellite) {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
} else if (itemId == R.id.mapTypeHybrid) {
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else if (itemId == R.id.gotoCurrentLocation) {
gotoCurrentLocation();
} else {
return super.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
The issue is that all of your R.id values are not actually final as they should be. In your R file, you'd find that they are something like public static int sample=0x7f035054;
.
The solution to that issue is using if statements. The code you have provided the second time SHOULD work. You can't possibly be getting an "case expressions must be constant expressions" if you had removed the switch statement and used those if statements instead.
Try restarting your IDE and rebuilding the project, with the switch statement definitely replaced by the if statements.