didi/DoKit · error · IllegalArgumentException

Width must be positive number or 0.

Error message

Width must be positive number or 0.

What it means

Thrown by Request.Builder.resize(int, int) when targetWidth is negative. Picasso treats 0 as 'keep aspect ratio' for a dimension, so any value below 0 is rejected as meaningless for scaling. The check fires immediately at the resize() call, before build().

Source

Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/Request.java:301

      return this;
    }

    /**
     * Set the stable key to be used instead of the URI or resource ID when caching.
     * Two requests with the same value are considered to be for the same resource.
     */
    public Builder stableKey(String stableKey) {
      this.stableKey = stableKey;
      return this;
    }

    /**
     * Resize the image to the specified size in pixels.
     * Use 0 as desired dimension to resize keeping aspect ratio.
     */
    public Builder resize(int targetWidth, int targetHeight) {
      if (targetWidth < 0) {
        throw new IllegalArgumentException("Width must be positive number or 0.");
      }
      if (targetHeight < 0) {
        throw new IllegalArgumentException("Height must be positive number or 0.");
      }
      if (targetHeight == 0 && targetWidth == 0) {
        throw new IllegalArgumentException("At least one dimension has to be positive number.");
      }
      this.targetWidth = targetWidth;
      this.targetHeight = targetHeight;
      return this;
    }

    /** Clear the resize transformation, if any. This will also clear center crop/inside if set. */
    public Builder clearResize() {
      targetWidth = 0;
      targetHeight = 0;
      centerCrop = false;
      centerInside = false;

View on GitHub (pinned to 626827cddb)

Solutions

  1. Clamp negative values to 0 before calling resize (0 legally means 'keep aspect ratio'): resize(Math.max(0, w), Math.max(0, h)).
  2. Never pass MATCH_PARENT/WRAP_CONTENT; resolve the view's measured size first (view.getWidth() after layout) or use getMeasuredWidth().
  3. If the size is unknown at request time, defer the request (Picasso's fit()) or pass only one dimension as 0.

Example fix

// before
int w = imageView.getLayoutParams().width; // may be -1 (MATCH_PARENT)
picasso.load(url).resize(w, 400).into(imageView);

// after
int w = Math.max(0, imageView.getWidth()); // measured pixels, 0 = keep ratio
picasso.load(url).resize(w, 400).into(imageView);
Defensive patterns

Strategy: validation

Validate before calling

int safeWidth = Math.max(0, targetWidth); // 0 = keep aspect ratio
builder.resize(safeWidth, targetHeight);

Prevention

When it happens

Trigger: Calling resize(-1, 100) or any resize() where the first argument is < 0 on a RequestCreator/Request.Builder.

Common situations: Passing view layout constants such as ViewGroup.LayoutParams.MATCH_PARENT (-1) or WRAP_CONTENT (-2) directly as pixel sizes; computing width from an unmeasured view (getWidth() returns 0 or a negative derived value); forwarding a dimension from code that uses -1 as 'unspecified'.

Related errors


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