{"record":{"id":"03a09823798d5004","repo":"DrKLO/Telegram","slug":"span-count-should-be-at-least-1-provided","errorCode":null,"errorMessage":"Span count should be at least 1. Provided {}","messagePattern":"Span count should be at least 1\\. Provided (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/GridLayoutManager.java","lineNumber":807,"sourceCode":"        return mSpanCount;\n    }\n\n    /**\n     * Sets the number of spans to be laid out.\n     * <p>\n     * If {@link #getOrientation()} is {@link #VERTICAL}, this is the number of columns.\n     * If {@link #getOrientation()} is {@link #HORIZONTAL}, this is the number of rows.\n     *\n     * @param spanCount The total number of spans in the grid\n     * @see #getSpanCount()\n     */\n    public void setSpanCount(int spanCount) {\n        if (spanCount == mSpanCount) {\n            return;\n        }\n        mPendingSpanCountChange = true;\n        if (spanCount < 1) {\n            throw new IllegalArgumentException(\"Span count should be at least 1. Provided \"\n                    + spanCount);\n        }\n        mSpanCount = spanCount;\n        mSpanSizeLookup.invalidateSpanIndexCache();\n        requestLayout();\n    }\n\n    /**\n     * A helper class to provide the number of spans each item occupies.\n     * <p>\n     * Default implementation sets each item to occupy exactly 1 span.\n     *\n     * @see GridLayoutManager#setSpanSizeLookup(SpanSizeLookup)\n     */\n    public abstract static class SpanSizeLookup {\n\n        final SparseIntArray mSpanIndexCache = new SparseIntArray();\n        final SparseIntArray mSpanGroupIndexCache = new SparseIntArray();","sourceCodeStart":789,"sourceCodeEnd":825,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/GridLayoutManager.java#L789-L825","documentation":"GridLayoutManager organizes items into exactly spanCount spans (rows or columns). A span count of 0 or negative makes the grid undefined (no lanes to place items into, division by zero in span math), so setSpanCount enforces a minimum of 1. The check runs on construction and on runtime changes; mSpanCount is used pervasively in division/loop bounds, so an invalid value would corrupt layout immediately. The most common trigger is a computed span count (from screen width or orientation) that evaluated to 0 because of an unfetched resource or a not-yet-laid-out view.","triggerScenarios":"Computing spanCount as displayMetrics.widthPixels / itemWidthPx before display metrics are available (returns 0); passing a value from preferences/JSON that is 0; a calculation like (count - N) that goes to 0 or negative for small counts; calling setSpanCount during construction with a resource-derived value that defaulted to 0.","commonSituations":"Responsive grid that calculates columns from screen width but runs before onGlobalLayout; orientation change recomputing span count before the new metrics load; a settings screen where the user could set columns to 0; dividing by a dp-to-px factor that resolved to 0.","solutions":["Clamp the computed span count: setSpanCount(Math.max(1, computed)).","Defer setSpanCount until after View.Layout/ViewTreeObserver reports real dimensions.","Validate any external/user-provided span count against >= 1 before applying."],"exampleFix":"// before\nint columns = displayMetrics.widthPixels / itemWidthPx; // 0 if metrics stale\nnew GridLayoutManager(this, columns); // throws\n\n// after\nint columns = Math.max(1, displayMetrics.widthPixels / itemWidthPx);\nnew GridLayoutManager(this, columns);","handlingStrategy":"validation","validationCode":"// Clamp span count to at least 1\nint safeSpanCount(int displayWidth, int itemWidthPx) {\n    return Math.max(1, displayWidth / Math.max(1, itemWidthPx));\n}\nnew GridLayoutManager(this, safeSpanCount(widthPx, itemPx));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always wrap computed span counts in Math.max(1, value).","Defer setSpanCount until real display metrics are available.","Validate user/settings-derived span counts against >= 1."],"tags":["recyclerview","gridlayoutmanager","configuration","spancount","responsive"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}