microg/GmsCore · error · IllegalStateException
MapValue is not a float
Error message
MapValue is not a float
What it means
Thrown by MapValue.asFloat() when the value's format is not FORMAT_FLOAT. MapValue wraps int or float values in fitness data; a given field holds exactly one format, so calling asFloat on an integer-formatted (or otherwise non-float) value is an invalid read of the wrapped data.
Source
Thrown at play-services-fitness/src/main/java/com/google/android/gms/fitness/data/MapValue.java:45
this.format = format;
this.value = value;
}
@NonNull
public static MapValue ofFloat(float value) {
return new MapValue(FORMAT_FLOAT, value);
}
public int getFormat() {
return format;
}
float getValue() {
return value;
}
public float asFloat() {
if (format != FORMAT_FLOAT) throw new IllegalStateException("MapValue is not a float");
return value;
}
@Override
public int hashCode() {
return (int) value;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
CREATOR.writeToParcel(this, dest, flags);
}
public static final SafeParcelableCreatorAndWriter<MapValue> CREATOR = findCreator(MapValue.class);
}
View on GitHub (pinned to 157c9d86ac)
Solutions
- Call asInt() for FORMAT_INT values instead of asFloat().
- Check mapValue.getFormat() == Field.FORMAT_FLOAT before calling asFloat().
- Read values via dataPoint.getValue(field) and branch on getFormat().
- Consult the DataType documentation to know each field's expected format.
Example fix
// before int steps = dataPoint.getValue(Field.FIELD_STEPS).asFloat(); // wrong // after MapValue v = dataPoint.getValue(Field.FIELD_STEPS); int steps = (v.getFormat() == Field.FORMAT_INT) ? v.asInt() : (int) v.asFloat();
Defensive patterns
Strategy: type-guard
Validate before calling
if (mapValue.getFormat() != Field.FORMAT_FLOAT) { /* use asInt() or skip */ } Type guard
Float asFloatIfFloat(MapValue v) { return v.getFormat() == Field.FORMAT_FLOAT ? v.asFloat() : null; } Try / catch
try { f = mapValue.asFloat(); } catch (IllegalStateException e) { f = (float) mapValue.asInt(); } Prevention
- Branch on getFormat() before reading MapValue values.
- Use asInt() for integer fields like FIELD_STEPS.
- Consult the DataType field table for expected formats.
- Centralize value reading in a helper that handles both formats.
When it happens
Trigger: Calling mapValue.asFloat() on a value obtained from a field whose format is FORMAT_INT (or another non-float format) — e.g. reading Field.FIELD_STEPS' value and calling asFloat instead of asInt.
Common situations: Copy-pasted value-reading code using asFloat for every field; fields whose format differs across data types (steps are ints, calories are floats); code written for one DataType reused with another whose fields are integer-valued.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- remoteBinder is the wrong class.
- The number of values does not match the number of fields
- Conflicting data sources found
- DataSet has already been built.
- dataType must be set
AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06).
Data as JSON: /api/errors/9bc44c2bc0ce09ab.
Report an issue: GitHub.