Tencent/QMUI_Android · error · IllegalArgumentException

thumbView must be a instance of View

Error message

thumbView must be a instance of View

What it means

In its constructor, QMUISlider creates the thumb view via onCreateThumbView and requires the returned IThumbView to also be a View instance, since it must be attached as a child view with an offset helper. A custom thumb that implements IThumbView but doesn't extend View triggers this IllegalArgumentException.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/widget/QMUISlider.java:130

                R.styleable.QMUISlider_qmui_slider_bar_use_clip_children_by_developer, false);
        if (!useClipChildrenByDeveloper) {
            int paddingHor = array.getDimensionPixelOffset(
                    R.styleable.QMUISlider_qmui_slider_bar_padding_hor_for_thumb_shadow, 0);
            int paddingVer = array.getDimensionPixelOffset(
                    R.styleable.QMUISlider_qmui_slider_bar_padding_ver_for_thumb_shadow, 0);
            setPadding(paddingHor, paddingVer, paddingHor, paddingVer);
        }
        array.recycle();
        mBarPaint = new Paint();
        mBarPaint.setStyle(Paint.Style.FILL);
        mBarPaint.setAntiAlias(true);
        mTouchSlop = QMUIDisplayHelper.dp2px(context, 2);
        setWillNotDraw(false);
        setClipToPadding(false);
        setClipChildren(false);
        IThumbView thumbView = onCreateThumbView(context, thumbSize, thumbStyleAttr);
        if (!(thumbView instanceof View)) {
            throw new IllegalArgumentException("thumbView must be a instance of View");
        }
        mThumbView = thumbView;
        View thumbAsView = (View) thumbView;
        mThumbViewOffsetHelper = new QMUIViewOffsetHelper(thumbAsView);
        addView(thumbAsView, onCreateThumbLayoutParams());
        thumbView.render(mCurrentProgress, mTickCount);
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    public void setCurrentProgress(int currentProgress) {
        if (!mIsMoving) {
            int progress = QMUILangHelper.constrain(currentProgress, 0, mTickCount);
            if (mCurrentProgress != progress || !mIsProgressFirstSet) {
                mIsProgressFirstSet = true;
                safeSetCurrentProgress(progress);

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Make the custom thumb class extend View (e.g. View or a drawable-backed custom View) while implementing IThumbView.
  2. Wrap your renderer inside a small View subclass that delegates rendering in onDraw().
  3. If you don't need a custom thumb, don't override onCreateThumbView and use the default implementation.
  4. Cast-check with instanceof View in your override before returning to fail fast in your own code.

Example fix

// before
protected IThumbView onCreateThumbView(Context c, int size, int attr) {
    return new MyThumbRenderer(); // not a View -> throws
}
// after
protected IThumbView onCreateThumbView(Context c, int size, int attr) {
    return new MyThumbView(c); // class MyThumbView extends View implements IThumbView
}
Defensive patterns

Strategy: type-guard

Validate before calling

IThumbView tv = onCreateThumbView(context, size, attr);
if (!(tv instanceof View)) {
    throw new IllegalStateException("Custom thumb must extend View");
}

Type guard

boolean isValidThumb(IThumbView t) { return t instanceof View; }

Try / catch

try {
    QMUISlider slider = new MySlider(context, thumbSize, thumbAttr);
} catch (IllegalArgumentException e) {
    Log.e(TAG, "onCreateThumbView must return a View-backed IThumbView", e);
}

Prevention

When it happens

Trigger: Subclassing QMUISlider and overriding onCreateThumbView(...) to return a custom IThumbView implementation that is not a View subclass.

Common situations: Custom thumb drawn with a separate renderer/Canvas object not tied to a View, or copying an IThumbView implementation from non-View widget code.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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