{"record":{"id":"5d87746f39d7df95","repo":"DrKLO/Telegram","slug":"cannot-call-this-method-unless-recyclerview-is-com","errorCode":null,"errorMessage":"Cannot call this method unless RecyclerView is computing a layout or scrolling","messagePattern":"Cannot call this method unless RecyclerView is computing a layout or scrolling","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java","lineNumber":3039,"sourceCode":"    /**\n     * Returns true if RecyclerView is attached to window.\n     */\n    @Override\n    public boolean isAttachedToWindow() {\n        return mIsAttached;\n    }\n\n    /**\n     * Checks if RecyclerView is in the middle of a layout or scroll and throws an\n     * {@link IllegalStateException} if it <b>is not</b>.\n     *\n     * @param message The message for the exception. Can be null.\n     * @see #assertNotInLayoutOrScroll(String)\n     */\n    void assertInLayoutOrScroll(String message) {\n        if (!isComputingLayout()) {\n            if (message == null) {\n                throw new IllegalStateException(\"Cannot call this method unless RecyclerView is \"\n                        + \"computing a layout or scrolling\" + exceptionLabel());\n            }\n            throw new IllegalStateException(message + exceptionLabel());\n\n        }\n    }\n\n    /**\n     * Checks if RecyclerView is in the middle of a layout or scroll and throws an\n     * {@link IllegalStateException} if it <b>is</b>.\n     *\n     * @param message The message for the exception. Can be null.\n     * @see #assertInLayoutOrScroll(String)\n     */\n    void assertNotInLayoutOrScroll(String message) {\n        if (isComputingLayout()) {\n            if (message == null) {\n                throw new IllegalStateException(\"Cannot call this method while RecyclerView is \"","sourceCodeStart":3021,"sourceCodeEnd":3057,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java#L3021-L3057","documentation":"assertInLayoutOrScroll(null) is the guard used by methods that may ONLY run while RecyclerView is computing a layout or scrolling (e.g. adapter notify calls inside onLayoutChildren, view measurement helpers). If isComputingLayout() is false and no custom message was supplied, it throws the generic 'Cannot call this method unless RecyclerView is computing a layout or scrolling'. The call must happen inside the layout/scroll window.","triggerScenarios":"Calling a layout-time-only API (e.g. layoutManager.findViewByPosition, certain offsetDescendants methods, scrap interactions) from outside onLayout/onScroll — e.g. from a click handler, an async callback, or onResume before the first layout pass.","commonSituations":"Calling notifyDataSetChanged or scroll-dependent lookups from a coroutine/Handler post that races ahead of layout. Invoking LayoutManager helper methods during initialization before the RecyclerView is attached.","solutions":["Move the call inside onLayoutChildren / onScrollChanged / a layout-pass callback where isComputingLayout() is true.","If you need the result after layout, post the call to the next frame via recyclerView.post(...) once layout is done.","Guard with: if (recyclerView.isComputingLayout()) { ... } else { postpone; }."],"exampleFix":"// before\nView v = layoutManager.findViewByPosition(pos); // called from onClick\n\n// after\nrecyclerView.post(() -> {\n    View v = layoutManager.findViewByPosition(pos);\n});","handlingStrategy":"validation","validationCode":"if (recyclerView.isComputingLayout()) {\n    // safe to call layout-time-only APIs\n    doLayoutTimeWork();\n} else {\n    recyclerView.post(this::doLayoutTimeWork);\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Check isComputingLayout() before calling layout-time-only helpers.","Post deferred work to the next frame when not in a layout pass.","Avoid invoking LayoutManager helpers from click handlers and async callbacks directly."],"tags":["recyclerview","lifecycle","layout-pass","threading"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}