didi/DoKit · error · RuntimeException

failed to get size

Error message

failed to get size

What it means

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.

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/kit/toolpanel/decoration/HorizontalDividerItemDecoration.java:212

        if (mPositionInsideItem) {
            outRect.set(0, 0, 0, 0);
            return;
        }
        outRect.set(0, 0, 0, getDividerSize(position, parent));
    }

    private int getDividerSize(int position, RecyclerView parent) {
        if (mPaintProvider != null) {
            return (int) mPaintProvider.dividerPaint(position, parent).getStrokeWidth();
        } else if (mSizeProvider != null) {
            return mSizeProvider.dividerSize(position, parent);
        } else if (mDrawableProvider != null) {
            Drawable drawable = mDrawableProvider.drawableProvider(position, parent);
            return drawable.getIntrinsicHeight();
        } else if (mSpaceProvider != null) {
            return mSpaceProvider.dividerSize(position, parent);
        }
        throw new RuntimeException("failed to get size");
    }

    /**
     * Interface for controlling divider margin
     */
    public interface MarginProvider {

        /**
         * Returns left margin of divider.
         *
         * @param position Divider position (or group index for GridLayoutManager)
         * @param parent   RecyclerView
         * @return left margin
         */
        int dividerLeftMargin(int position, RecyclerView parent);

        /**
         * Returns right margin of divider.

View on GitHub (pinned to 626827cddb)

Solutions

  1. Configure at least one provider before build(), e.g. .sizeProvider(new SizeProvider() { public int dividerSize(int position, RecyclerView parent) { return 1; } })
  2. Or supply a drawable: .drawableProvider(...) whose Drawable reports a non-zero intrinsic height
  3. If you never want a visible divider, remove the decoration from the RecyclerView instead of leaving it provider-less
  4. Verify with a debug assert right after build() that the relevant provider field is non-null

Example fix

// before
RecyclerView.ItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context).build();
recyclerView.addItemDecoration(deco);

// after
RecyclerView.ItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context)
    .sizeProvider(new HorizontalDividerItemDecoration.SizeProvider() {
      @Override public int dividerSize(int position, RecyclerView parent) {
        return (int) (context.getResources().getDisplayMetrics().density * 1); // 1dp
      }
    })
    .build();
recyclerView.addItemDecoration(deco);
Defensive patterns

Strategy: validation

Validate before calling

// Before build(), ensure one provider is set — simplest guard is to always set a size provider.
HorizontalDividerItemDecoration deco = new HorizontalDividerItemDecoration.Builder(context)
    .sizeProvider((position, parent) -> Math.round(1 * context.getResources().getDisplayMetrics().density))
    .marginProvider((position, parent) -> 0)
    .build();
recyclerView.addItemDecoration(deco);

Try / catch

// Last-resort guard during layout (crashes in layout pass, so prevention is better):
try {
  recyclerView.addItemDecoration(deco);
} catch (RuntimeException e) {
  if (e.getMessage() != null && e.getMessage().contains("failed to get size")) {
    Log.w(TAG, "Divider built without a provider; skipping decoration", e);
  } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/8dd9ced70d4f506f. Report an issue: GitHub.