Tencent/QMUI_Android · error · IllegalArgumentException

You need to use a Theme.AppCompat theme (or descendant) with

Error message

You need to use a Theme.AppCompat theme (or descendant) with the design library.

What it means

QMUIViewHelper.checkAppCompatTheme verifies that the context's theme defines the AppCompat flag attribute (APPCOMPAT_CHECK_ATTRS). If the attribute is missing, the activity/theme is not a Theme.AppCompat descendant, and the design library components would crash or render incorrectly, so an IllegalArgumentException is thrown.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/util/QMUIViewHelper.java:80

 * @author cginechen
 * @date 2016-03-17
 */
public class QMUIViewHelper {

    // copy from View.generateViewId for API <= 16
    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);


    private static final int[] APPCOMPAT_CHECK_ATTRS = {
            androidx.appcompat.R.attr.colorPrimary
    };

    public static void checkAppCompatTheme(Context context) {
        TypedArray a = context.obtainStyledAttributes(APPCOMPAT_CHECK_ATTRS);
        final boolean failed = !a.hasValue(0);
        a.recycle();
        if (failed) {
            throw new IllegalArgumentException("You need to use a Theme.AppCompat theme "
                    + "(or descendant) with the design library.");
        }
    }

    /**
     * 获取activity的根view
     */
    public static View getActivityRoot(Activity activity) {
        return ((ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT)).getChildAt(0);
    }

    /**
     * 触发window的insets的广播,使得view的fitSystemWindows得以生效
     */
    @SuppressWarnings("deprecation")
    public static void requestApplyInsets(Window window) {
        if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
            window.getDecorView().requestFitSystemWindows();

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Change the activity's theme in AndroidManifest.xml to one extending Theme.AppCompat (or Theme.MaterialComponents / DayNight).
  2. Make your app theme's parent "Theme.AppCompat.Light.DarkActionBar" or similar.
  3. If the activity intentionally isn't AppCompat, apply a ContextThemeWrapper with an AppCompat theme when creating the view.
  4. Check styles.xml for themes accidentally declared with parent="android:Theme.*" instead of "Theme.AppCompat.*".

Example fix

// before (AndroidManifest.xml)
<activity android:name=".MainActivity" android:theme="@style/OldHoloTheme" />
// after (styles.xml + manifest)
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" />
<activity android:name=".MainActivity" android:theme="@style/AppTheme" />
Defensive patterns

Strategy: validation

Validate before calling

TypedArray a = context.obtainStyledAttributes(
        new int[]{R.attr.appcompat_flag_attr_placeholder});
boolean ok = a.hasValue(0);
a.recycle();
if (!ok) Log.e(TAG, "Theme is not AppCompat; fix styles.xml before using QMUI design widgets");

Try / catch

try {
    QMUIViewHelper.checkAppCompatTheme(context);
} catch (IllegalArgumentException e) {
    throw new IllegalStateException("Set android:theme to a Theme.AppCompat descendant", e);
}

Prevention

When it happens

Trigger: Using QMUI design-library components (e.g. bottom sheets, widgets backed by AppCompat) in an Activity whose theme does not inherit from Theme.AppCompat / Theme.MaterialComponents, e.g. android:Theme.Holo or a plain platform theme.

Common situations: Setting a non-AppCompat theme in AndroidManifest.xml for a specific activity, migrating an old app to the design library without updating styles.xml, or using a ContextThemeWrapper with the wrong base theme.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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