material-components/material-components-android · error · IllegalArgumentException
index out of range
Error message
index out of range
What it means
IllegalArgumentException thrown synchronously by setFocusedThumbIndex(int) when the index is negative or >= values.size(). The focused thumb is the one highlighted and driven by keyboard/a11y interaction, so it must reference an existing thumb.
Source
Thrown at lib/java/com/google/android/material/slider/BaseSlider.java:1168
int originalHeight = drawable.getIntrinsicHeight();
if (originalWidth == -1 && originalHeight == -1) {
drawable.setBounds(0, 0, width, thumbHeight);
} else {
float scaleRatio = (float) max(width, thumbHeight) / max(originalWidth, originalHeight);
drawable.setBounds(
0, 0, (int) (originalWidth * scaleRatio), (int) (originalHeight * scaleRatio));
}
}
/** Returns the index of the currently focused thumb */
public int getFocusedThumbIndex() {
return focusedThumbIdx;
}
/** Sets the index of the currently focused thumb */
public void setFocusedThumbIndex(int index) {
if (index < 0 || index >= values.size()) {
throw new IllegalArgumentException("index out of range");
}
focusedThumbIdx = index;
accessibilityHelper.requestKeyboardFocusForVirtualView(focusedThumbIdx);
postInvalidate();
}
protected void setActiveThumbIndex(int index) {
activeThumbIdx = index;
}
/** Returns the index of the currently active thumb, or -1 if no thumb is active */
public int getActiveThumbIndex() {
return activeThumbIdx;
}
/**
* Registers a callback to be invoked when the slider changes. On the RangeSlider implementation,
* the listener is invoked once for each value.View on GitHub (pinned to ac7e18efee)
Solutions
- Bound-check before calling: 0 <= index < slider.getValues().size().
- Re-apply focus after setValues(), not before, since values.size() may have changed.
- Skip the call (or clamp to size-1) instead of passing -1 when 'no thumb' is desired.
Example fix
// before
slider.setFocusedThumbIndex(savedIndex); // crashes if thumb count shrank
// after
if (savedIndex >= 0 && savedIndex < slider.getValues().size()) {
slider.setFocusedThumbIndex(savedIndex);
} Defensive patterns
Strategy: validation
Validate before calling
if (index >= 0 && index < slider.getValues().size()) {
slider.setFocusedThumbIndex(index);
} Prevention
- Re-apply the focused index after setValues(), not before.
- Persist the focused index together with the value count and validate on restore.
When it happens
Trigger: Calling setFocusedThumbIndex(1) on a single-value slider; caching a focus index across a call to setValues() that reduces the thumb count; passing -1 as 'no focus' (use clearFocusedThumbIndex-equivalent behavior or skip the call instead).
Common situations: Restoring UI state after process death when the number of thumbs changed; accessibility flows that remember the last focused thumb; index math based on a stale values list.
Related errors
- At least one value must be set
- The continuousModeTickCount(%s) must be greater than or equa
- The stepSize(%s) must be 0, or a factor of the valueFrom(%s)
- There must be a keyline marked as focal.
- No color resources provided for harmonization.
AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14).
Data as JSON: /api/errors/59125cdc8c3d93b1.
Report an issue: GitHub.