Tencent/QMUI_Android · error · IllegalArgumentException

index must greater than 0

Error message

index must greater than 0

What it means

QMUISkinManager.addSkin(index, styleRes) requires the skin index to be strictly positive; index 0 is reserved (DEFAULT_SKIN) and negative indices are invalid. Passing index <= 0 is rejected with an IllegalArgumentException before the skin is stored.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/skin/QMUISkinManager.java:244

        if (skinItem != null) {
            return skinItem.getTheme();
        }
        return null;
    }

    @Nullable
    public Resources.Theme getCurrentTheme() {
        SkinItem skinItem = mSkins.get(mCurrentSkin);
        if (skinItem != null) {
            return skinItem.getTheme();
        }
        return null;
    }

    @MainThread
    public void addSkin(int index, int styleRes) {
        if (index <= 0) {
            throw new IllegalArgumentException("index must greater than 0");
        }
        SkinItem skinItem = mSkins.get(index);
        if (skinItem != null) {
            if (skinItem.getStyleRes() == styleRes) {
                return;
            }
            throw new RuntimeException("already exist the theme item for " + index);
        }
        skinItem = new SkinItem(styleRes);
        mSkins.append(index, skinItem);
    }

    static ViewSkinCurrent getViewSkinCurrent(View view) {
        Object current = view.getTag(R.id.qmui_skin_current);
        if (current instanceof ViewSkinCurrent) {
            return (ViewSkinCurrent) current;
        }
        return null;

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Use indices starting at 1 for custom skins.
  2. To customize the default skin, use the style/theme mechanism for DEFAULT_SKIN instead of addSkin(0, ...).
  3. Fix loops to iterate from 1, or add 1 to array-based indices.

Example fix

// before
skinManager.addSkin(0, R.style.AppSkin_Blue); // throws

// after
skinManager.addSkin(1, R.style.AppSkin_Blue);
Defensive patterns

Strategy: validation

Validate before calling

if (index > 0) {
    skinManager.addSkin(index, styleRes);
}

Type guard

boolean isSkinIndexValid(int index) {
    return index > 0;
}

Try / catch

try {
    skinManager.addSkin(index, styleRes);
} catch (IllegalArgumentException e) {
    Log.w(TAG, "invalid skin index: " + index, e);
}

Prevention

When it happens

Trigger: Calling addSkin(0, R.style.SkinX) or addSkin(-1, ...) directly, or building a loop that starts at 0 when registering skins after install().

Common situations: Misunderstanding that index 0 means 'default skin' and trying to register the default theme; generating skin indices from an array position starting at 0.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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