didi/DoKit · error · IllegalStateException
Fit cannot be used with resize.
Error message
Fit cannot be used with resize.
What it means
Thrown by RequestCreator.into(ImageView, Callback) when the request uses both fit() and resize(). fit() means 'defer sizing until the target ImageView is laid out' (deferred=true); resize() means 'use these exact dimensions' (data.hasSize()). The two are mutually exclusive sizing strategies, and the library throws IllegalStateException during the deferred branch of into() rather than guessing which one wins.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/RequestCreator.java:650
public void into(ImageView target, Callback callback) {
long started = System.nanoTime();
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
if (!data.hasImage()) {
picasso.cancelRequest(target);
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
return;
}
if (deferred) {
if (data.hasSize()) {
throw new IllegalStateException("Fit cannot be used with resize.");
}
int width = target.getWidth();
int height = target.getHeight();
if (width == 0 || height == 0) {
if (setPlaceholder) {
setPlaceholder(target, getPlaceholderDrawable());
}
picasso.defer(target, new DeferredRequestCreator(this, target, callback));
return;
}
data.resize(width, height);
}
Request request = createRequest(started);
String requestKey = createKey(request);
if (shouldReadFromMemoryCache(memoryPolicy)) {
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);View on GitHub (pinned to 626827cddb)
Solutions
- Pick one sizing strategy: remove resize() if you want fit()'s layout-measured size, or remove fit() if the fixed size is correct.
- Prefer fit() when the ImageView size varies by device/screen; prefer resize() (+ centerInside/onlyScaleDown) when you know target dimensions.
- Audit shared request-builder helpers so they do not silently add resize to requests that call sites extend with fit().
Example fix
// before picasso.load(url).fit().resize(200, 200).into(imageView); // after picasso.load(url).fit().into(imageView); // or fixed size: picasso.load(url).resize(200, 200).centerInside().into(imageView);
Defensive patterns
Strategy: validation
Validate before calling
// Centralize sizing policy
RequestCreator withSizing(Picasso p, String url, boolean fitToView) {
RequestCreator rc = p.load(url);
if (fitToView) {
return rc.fit();
}
return rc.resize(DEFAULT_W, DEFAULT_H).centerInside();
} Try / catch
Not applicable — IllegalStateException signals deterministic builder conflict; correct the chain.
Prevention
- fit() and resize() are mutually exclusive; enforce one per request in code review.
- Shared builder factories must not mix sizing strategies across layers.
- Prefer fit() for unknown container sizes, resize() for fixed cells.
When it happens
Trigger: Chaining .fit().resize(w, h) (or .resize(...) then .fit()) on the same RequestCreator and calling into(imageView); often happens when a shared builder sets resize and call-site code adds fit(), or vice versa.
Common situations: A defaults layer (request transformer or shared factory) applying resize for memory efficiency while a screen adds fit() for exact container matching; copy-pasting load code and appending fit() to a line that already resizes.
Related errors
- Priority already set.
- Error image already set.
- Fit cannot be used with RemoteViews.
- Cannot use placeholder or error drawables with remote views.
- Fit cannot be used with remote views.
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/353eb34099e53f25.
Report an issue: GitHub.