Tencent/QMUI_Android · error · IllegalArgumentException

delegateView must be a instance of View

Error message

delegateView must be a instance of View

What it means

QMUIContinuousNestedTopDelegateLayout.setDelegateView() declares its parameter as IQMUIContinuousNestedTopView but defensively checks that it is also an android.view.View before calling addView/casting. In practice the instanceof View check is redundant for any normal implementation, so this IllegalArgumentException almost always signals a broken or null-backed implementation of the interface.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/nestedScroll/QMUIContinuousNestedTopDelegateLayout.java:87

                                                 @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mParentHelper = new NestedScrollingParentHelper(this);
        mChildHelper = new NestedScrollingChildHelper(this);

        ViewCompat.setNestedScrollingEnabled(this, true);
        setClipToPadding(false);
    }

    public void setHeaderView(@NonNull View headerView) {
        mHeaderView = headerView;
        mHeaderViewOffsetHelper = new QMUIViewOffsetHelper(headerView);
        addView(headerView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    public void setDelegateView(@NonNull IQMUIContinuousNestedTopView delegateView) {
        if (!(delegateView instanceof View)) {
            throw new IllegalArgumentException("delegateView must be a instance of View");
        }
        if (mDelegateView != null) {
            mDelegateView.injectScrollNotifier(null);
        }
        mDelegateView = delegateView;
        View view = (View) delegateView;
        mDelegateViewOffsetHelper = new QMUIViewOffsetHelper(view);
        // WRAP_CONTENT, the height will be handled by QMUIContinuousNestedTopAreaBehavior
        addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public void setFooterView(@NonNull View footerView) {
        mFooterView = footerView;
        mFooterViewOffsetHelper = new QMUIViewOffsetHelper(footerView);
        addView(footerView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    @Override

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Make the delegate a real View: extend a View/ViewGroup class AND implement IQMUIContinuousNestedTopView.
  2. Use QMUI's stock implementations (QMUIContinuousNestedTopScrollLayout, QMUIContinuousNestedTopRecyclerView, QMUIContinuousNestedTopWebView).
  3. In tests, pass a real View-based instance (or Robolectric view) instead of a mock implementing only the interface.

Example fix

// before
IQMUIContinuousNestedTopView delegate = mock(IQMUIContinuousNestedTopView.class); // not a View
delegateLayout.setDelegateView(delegate);

// after
IQMUIContinuousNestedTopView delegate = new QMUIContinuousNestedTopScrollLayout(context);
delegateLayout.setDelegateView(delegate);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(delegateView instanceof View)) {
    throw new IllegalArgumentException("delegateView must be a View");
}
delegateLayout.setDelegateView(delegateView);

Type guard

boolean isDelegateViewValid(IQMUIContinuousNestedTopView v) {
    return v instanceof View;
}

Try / catch

try {
    delegateLayout.setDelegateView(delegateView);
} catch (IllegalArgumentException e) {
    Log.e(TAG, "delegateView is not a View", e);
}

Prevention

When it happens

Trigger: Calling setDelegateView() with an IQMUIContinuousNestedTopView implementation that is not a View subclass — e.g. a proxy/mock implementation of the interface, or a dynamically generated implementation — or a null proxy passing the generic check.

Common situations: Seen in unit tests using Mockito/dynamic proxies for IQMUIContinuousNestedTopView, or extremely unusual custom implementations that extend Object only instead of a View class.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of Tencent/QMUI_Android@026e7d4866 (2026-09-06). Data as JSON: /api/errors/f9302ffa1b656864. Report an issue: GitHub.