{"record":{"id":"8dd9ced70d4f506f","repo":"didi/DoKit","slug":"failed-to-get-size","errorCode":null,"errorMessage":"failed to get size","messagePattern":"failed to get size","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/toolpanel/decoration/HorizontalDividerItemDecoration.java","lineNumber":212,"sourceCode":"        if (mPositionInsideItem) {\n            outRect.set(0, 0, 0, 0);\n            return;\n        }\n        outRect.set(0, 0, 0, getDividerSize(position, parent));\n    }\n\n    private int getDividerSize(int position, RecyclerView parent) {\n        if (mPaintProvider != null) {\n            return (int) mPaintProvider.dividerPaint(position, parent).getStrokeWidth();\n        } else if (mSizeProvider != null) {\n            return mSizeProvider.dividerSize(position, parent);\n        } else if (mDrawableProvider != null) {\n            Drawable drawable = mDrawableProvider.drawableProvider(position, parent);\n            return drawable.getIntrinsicHeight();\n        } else if (mSpaceProvider != null) {\n            return mSpaceProvider.dividerSize(position, parent);\n        }\n        throw new RuntimeException(\"failed to get size\");\n    }\n\n    /**\n     * Interface for controlling divider margin\n     */\n    public interface MarginProvider {\n\n        /**\n         * Returns left margin of divider.\n         *\n         * @param position Divider position (or group index for GridLayoutManager)\n         * @param parent   RecyclerView\n         * @return left margin\n         */\n        int dividerLeftMargin(int position, RecyclerView parent);\n\n        /**\n         * Returns right margin of divider.","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/toolpanel/decoration/HorizontalDividerItemDecoration.java#L194-L230","documentation":"Thrown by getDividerSize(int, RecyclerView) in HorizontalDividerItemDecoration when none of the four providers (mPaintProvider, mSizeProvider, mDrawableProvider, mSpaceProvider) is configured. The decoration cannot compute a divider height for getItemOffsets/getItemOffsets drawing, so it fails fast with a RuntimeException instead of guessing a size. It almost always means the decoration was created via new HorizontalDividerItemDecoration.Builder(context).build() without calling any of the builder's provider setters.","triggerScenarios":"Building the decoration with the no-arg/Builder constructor and never calling builder.paintProvider(...), .sizeProvider(...), .drawableProvider(...), or .spaceProvider(...); or keeping the provider in a field that is null at build time; then attaching the decoration to a RecyclerView so getItemOffsets() runs during layout.","commonSituations":"A DoraemonKit tool panel adds a divider and assumes a default size exists; refactoring removes the sizeProvider call; provider set after build() so the built instance never sees it.","solutions":["Configure at least one provider before build(), e.g. .sizeProvider(new SizeProvider() { public int dividerSize(int position, RecyclerView parent) { return 1; } })","Or supply a drawable: .drawableProvider(...) whose Drawable reports a non-zero intrinsic height","If you never want a visible divider, remove the decoration from the RecyclerView instead of leaving it provider-less","Verify with a debug assert right after build() that the relevant provider field is non-null"],"exampleFix":"// before\nRecyclerView.ItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context).build();\nrecyclerView.addItemDecoration(deco);\n\n// after\nRecyclerView.ItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context)\n    .sizeProvider(new HorizontalDividerItemDecoration.SizeProvider() {\n      @Override public int dividerSize(int position, RecyclerView parent) {\n        return (int) (context.getResources().getDisplayMetrics().density * 1); // 1dp\n      }\n    })\n    .build();\nrecyclerView.addItemDecoration(deco);","handlingStrategy":"validation","validationCode":"// Before build(), ensure one provider is set — simplest guard is to always set a size provider.\nHorizontalDividerItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context)\n    .sizeProvider((position, parent) -> Math.round(1 * context.getResources().getDisplayMetrics().density))\n    .marginProvider((position, parent) -> 0)\n    .build();\nrecyclerView.addItemDecoration(deco);","typeGuard":null,"tryCatchPattern":"// Last-resort guard during layout (crashes in layout pass, so prevention is better):\ntry {\n  recyclerView.addItemDecoration(deco);\n} catch (RuntimeException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"failed to get size\")) {\n    Log.w(TAG, \"Divider built without a provider; skipping decoration\", e);\n  } else throw e;\n}","preventionTips":["Never call build() on the divider Builder without at least one of sizeProvider/drawableProvider/paintProvider","Wrap divider construction in a small factory method that always applies defaults","Unit-test the factory: build the decoration and assert provider fields are non-null"],"tags":["android","recyclerview","ui","configuration"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}