{"record":{"id":"bb654b8faa34397c","repo":"DrKLO/Telegram","slug":"cannot-call-this-method-while-recyclerview-is-comp","errorCode":null,"errorMessage":"Cannot call this method while RecyclerView is computing a layout or scrolling","messagePattern":"Cannot call this method while 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":3057,"sourceCode":"                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 \"\n                        + \"computing a layout or scrolling\" + exceptionLabel());\n            }\n            throw new IllegalStateException(message);\n        }\n        if (mDispatchScrollCounter > 0) {\n            Log.w(TAG, \"Cannot call this method in a scroll callback. Scroll callbacks might\"\n                            + \"be run during a measure & layout pass where you cannot change the\"\n                            + \"RecyclerView data. Any method call that might change the structure\"\n                            + \"of the RecyclerView or the adapter contents should be postponed to\"\n                            + \"the next frame.\",\n                    new IllegalStateException(\"\" + exceptionLabel()));\n        }\n    }\n\n    /**\n     * Add an {@link OnItemTouchListener} to intercept touch events before they are dispatched\n     * to child views or this view's standard scrolling behavior.\n     *","sourceCodeStart":3039,"sourceCodeEnd":3075,"githubUrl":"https://github.com/DrKLO/Telegram/blob/45ab8f4308496e1f01026a97fcdb0d58a5274474/TMessagesProj/src/main/java/androidx/recyclerview/widget/RecyclerView.java#L3039-L3075","documentation":"assertNotInLayoutOrScroll(null) is the inverse guard: it forbids methods that must NOT run during a layout or scroll pass (e.g. setAdapter, setLayoutManager, setHasFixedSize, adapter data-set calls that re-trigger layout). If isComputingLayout() is true and no message was supplied, it throws 'Cannot call this method while RecyclerView is computing a layout or scrolling'. Re-entrant layout mutations corrupt the in-flight pass.","triggerScenarios":"Calling setAdapter, swapAdapter, setLayoutManager, or adapter.notify* from inside onLayoutChildren of a custom LayoutManager, from an ItemDecoration.onDraw, or from a scroll listener that triggers a data mutation.","commonSituations":"Adapter swap triggered inside a scroll listener (onScrolled) that itself runs during layout. Custom LayoutManager that calls recyclerView.setAdapter during layout. Infinite scroll that calls notifyDataSetChanged synchronously inside onScrolled.","solutions":["Defer structural changes out of the layout/scroll pass: use recyclerView.post(() -> adapter.notifyDataSetChanged()).","For pagination, set a pending flag in onScrolled and apply new data on the next frame.","Never call setAdapter/setLayoutManager from within onLayoutChildren or ItemDecoration callbacks."],"exampleFix":"// before\nrecyclerView.addOnScrollListener(new OnScrollListener() {\n    @Override public void onScrolled(RecyclerView rv, int dx, int dy) {\n        if (atBottom) adapter.loadMore(); // may call notify* during layout\n    }\n});\n\n// after\nrecyclerView.addOnScrollListener(new OnScrollListener() {\n    @Override public void onScrolled(RecyclerView rv, int dx, int dy) {\n        if (atBottom && !pendingLoad) {\n            pendingLoad = true;\n            rv.post(() -> { adapter.loadMore(); pendingLoad = false; });\n        }\n    }\n});","handlingStrategy":"validation","validationCode":"Runnable structuralChange = () -> {\n    adapter.notifyDataSetChanged(); // or setAdapter / setLayoutManager\n};\nif (recyclerView.isComputingLayout()) {\n    recyclerView.post(structuralChange);\n} else {\n    structuralChange.run();\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Never call setAdapter/setLayoutManager/notifyDataSetChanged from within onLayoutChildren or scroll listeners.","Defer pagination loads with a pending flag and apply on the next frame.","Assert !isComputingLayout() in debug builds before structural mutations."],"tags":["recyclerview","lifecycle","layout-pass","reentrancy"],"backgroundTag":null,"analyzedSha":"45ab8f4308496e1f01026a97fcdb0d58a5274474","analyzedAt":"2026-08-14T05:19:30.815Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}