{"record":{"id":"6b0d75827a886c16","repo":"material-components/material-components-android","slug":"invalid-state-to-get-top-offset","errorCode":null,"errorMessage":"Invalid state to get top offset: ","messagePattern":"Invalid state to get top offset: ","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java","lineNumber":2069,"sourceCode":"    } else {\n      setStateInternal(state);\n    }\n  }\n\n  private int getTopOffsetForState(@StableState int state) {\n    switch (state) {\n      case STATE_COLLAPSED:\n        return collapsedOffset;\n      case STATE_EXPANDED:\n        return getExpandedOffset();\n      case STATE_HALF_EXPANDED:\n        return halfExpandedOffset;\n      case STATE_HIDDEN:\n        return parentHeight;\n      default:\n        // Fall through\n    }\n    throw new IllegalArgumentException(\"Invalid state to get top offset: \" + state);\n  }\n\n  private final ViewDragHelper.Callback dragCallback =\n      new ViewDragHelper.Callback() {\n\n        private long viewCapturedMillis;\n\n        @Override\n        public boolean tryCaptureView(@NonNull View child, int pointerId) {\n          if (state == STATE_DRAGGING) {\n            return false;\n          }\n          if (touchingScrollingChild) {\n            return false;\n          }\n          if (state == STATE_EXPANDED && activePointerId == pointerId) {\n            View scroll;\n            if (multipleScrollingChildrenSupported) {","sourceCodeStart":2051,"sourceCodeEnd":2087,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/lib/java/com/google/android/material/bottomsheet/BottomSheetBehavior.java#L2051-L2087","documentation":"BottomSheetBehavior.getTopOffsetForState(int) is an internal helper that maps a stable target state to its pixel top offset (collapsedOffset, expanded offset, halfExpandedOffset, or parentHeight for HIDDEN). Any state outside those four cases falls through the switch and throws IllegalArgumentException with the state value appended.","triggerScenarios":"Reaching this code via setState with an unexpected int (the setter routes stable states through getTopOffsetForState); internal calls like calculateHalfExpandedOffset when the behavior was constructed with an invalid state constant, e.g. a fabricated int passed instead of a STATE_* constant.","commonSituations":"Passing a raw int from storage/intent extras into setState without validating it is one of the STATE_* constants; deserializing a corrupted state value; using reflection or custom constants that collide with none of the cases.","solutions":["Only use the documented constants: STATE_COLLAPSED, STATE_EXPANDED, STATE_HALF_EXPANDED, STATE_HIDDEN (and never DRAGGING/SETTLING via setState).","Validate deserialized state values against the known set before calling setState.","If you must store state as an int, round-trip only values obtained from the STATE_* constants."],"exampleFix":"// before\nint state = prefs.getInt(\"sheet_state\", -1);\nbehavior.setState(state); // -1 -> \"Invalid state to get top offset: -1\"\n\n// after\nint state = prefs.getInt(\"sheet_state\", BottomSheetBehavior.STATE_COLLAPSED);\nif (state != STATE_COLLAPSED && state != STATE_EXPANDED\n    && state != STATE_HALF_EXPANDED && state != STATE_HIDDEN) {\n  state = BottomSheetBehavior.STATE_COLLAPSED;\n}\nbehavior.setState(state);","handlingStrategy":"validation","validationCode":"int s = readState();\nif (s != STATE_COLLAPSED && s != STATE_EXPANDED && s != STATE_HALF_EXPANDED && s != STATE_HIDDEN) {\n  s = BottomSheetBehavior.STATE_COLLAPSED;\n}\nbehavior.setState(s);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only round-trip ints obtained from STATE_* constants through storage.","Treat any unrecognized state int as corrupt data and reset to a safe default."],"tags":["bottomsheet","illegal-argument","state-machine","serialization"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}