didi/DoKit · error · IllegalStateException
Fit cannot be used with a Target.
Error message
Fit cannot be used with a Target.
What it means
Thrown by RequestCreator.into(Target) when the chain called fit(). fit() works only with an ImageView, whose measured bounds are used to size the request after layout; a Target provides no measurement source, so Picasso rejects the combination with IllegalStateException.
Source
Thrown at Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/RequestCreator.java:521
* {@literal @}Override public void onPrepareLoad(Drawable placeHolderDrawable) {
* frame.setBackgroundDrawable(placeHolderDrawable);
* }
* }
* </pre></blockquote>
* <p>
* <em>Note:</em> This method keeps a weak reference to the {@link Target} instance and will be
* garbage collected if you do not keep a strong reference to it. To receive callbacks when an
* image is loaded use {@link #into(android.widget.ImageView, Callback)}.
*/
public void into(Target target) {
long started = System.nanoTime();
checkMain();
if (target == null) {
throw new IllegalArgumentException("Target must not be null.");
}
if (deferred) {
throw new IllegalStateException("Fit cannot be used with a Target.");
}
if (!data.hasImage()) {
picasso.cancelRequest(target);
target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);
return;
}
Request request = createRequest(started);
String requestKey = createKey(request);
if (shouldReadFromMemoryCache(memoryPolicy)) {
Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);
if (bitmap != null) {
picasso.cancelRequest(target);
target.onBitmapLoaded(bitmap, MEMORY);
return;
}View on GitHub (pinned to 626827cddb)
Solutions
- Remove fit() and size explicitly: .resize(dimW, dimH).centerInside().into(target)
- Add a boolean or overload to the shared helper so fit() is applied only for ImageView loads
- If the target ultimately feeds an ImageView, use into(imageView) instead of a Target
Example fix
// before picasso.load(url).fit().into(notificationTarget); // throws "Fit cannot be used with a Target." // after picasso.load(url).resize(64, 64).centerCrop().into(notificationTarget);
Defensive patterns
Strategy: validation
Validate before calling
// Target loads need explicit sizing; never fit()
picasso.load(url)
.resize(iconSizePx, iconSizePx)
.centerCrop()
.into(target); Prevention
- Treat fit() as ImageView-only; document it on shared loading helpers
- Add an overload to shared helpers that skips fit() for Target/RemoteViews loads
- Compute the destination dimensions yourself (e.g. notification icon size) when no ImageView exists
When it happens
Trigger: Calling picasso.load(url).fit().into(target), e.g. loading into a custom Target (Notification icon, custom View, Android Wear tile) while reusing a chain built for ImageViews.
Common situations: Shared loading helpers that always apply fit() being reused for Target-based loads (app widgets, notifications, custom views); migrating into(imageView) code to a Target without dropping fit().
Related errors
- Fit cannot be used with get.
- Fit cannot be used with fetch.
- Target must not be null.
- Error image resource invalid.
- Error image already set.
AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14).
Data as JSON: /api/errors/58eb2f65fa5fe83c.
Report an issue: GitHub.