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

  1. Call asInt() for FORMAT_INT values instead of asFloat().
  2. Check mapValue.getFormat() == Field.FORMAT_FLOAT before calling asFloat().
  3. Read values via dataPoint.getValue(field) and branch on getFormat().
  4. 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

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


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/9bc44c2bc0ce09ab. Report an issue: GitHub.