Tencent/QMUI_Android · error · IllegalStateException

Use addContentView(int) for replacement

Error message

Use addContentView(int) for replacement

What it means

Same design as the View overload: QMUIBottomSheet cannot host a raw layout-resource content view, so setContentView(int layoutResId) is overridden to always throw IllegalStateException, telling you to use addContentView(int) (or the builder API) instead.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/widget/dialog/QMUIBottomSheet.java:255

            public void run() {
                mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });
    }

    public interface OnBottomSheetShowListener {
        void onShow();
    }

    @Override
    public void setContentView(View view) {
        throw new IllegalStateException(
                "Use addContentView(View, ConstraintLayout.LayoutParams) for replacement");
    }

    @Override
    public void setContentView(int layoutResId) {
        throw new IllegalStateException(
                "Use addContentView(int) for replacement");
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        throw new IllegalStateException(
                "Use addContentView(View, QMUIPriorityLinearLayout.LayoutParams) for replacement");
    }

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        throw new IllegalStateException(
                "Use addContentView(View, QMUIPriorityLinearLayout.LayoutParams) for replacement");
    }

    public void addContentView(View view, QMUIPriorityLinearLayout.LayoutParams layoutParams) {
        mRootView.addView(view, layoutParams);
    }

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Inflate the layout yourself and pass the view via addContentView with ConstraintLayout.LayoutParams.
  2. Use the QMUIBottomSheet builder (createBottomSheetBuilder().addView(inflater.inflate(R.layout.my_content, null))) instead.
  3. Call addContentView(view, ConstraintLayout.LayoutParams) with the inflated view.
  4. Refactor helpers to detect QMUIBottomSheet and avoid the forbidden overloads.

Example fix

// before
bottomSheet.setContentView(R.layout.sheet_content); // throws
// after
View view = LayoutInflater.from(context).inflate(R.layout.sheet_content, null);
bottomSheet.addContentView(view, new ConstraintLayout.LayoutParams(
        ConstraintLayout.LayoutParams.MATCH_PARENT,
        ConstraintLayout.LayoutParams.WRAP_CONTENT));
Defensive patterns

Strategy: try-catch

Validate before calling

if (sheet instanceof QMUIBottomSheet) {
    View v = LayoutInflater.from(context).inflate(layoutResId, null);
    sheet.addContentView(v, new ConstraintLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
} else {
    sheet.setContentView(layoutResId);
}

Type guard

boolean supportsSetContentView(Dialog d) { return !(d instanceof QMUIBottomSheet); }

Try / catch

try {
    sheet.setContentView(R.layout.sheet_content);
} catch (IllegalStateException e) {
    View v = LayoutInflater.from(context).inflate(R.layout.sheet_content, null);
    sheet.addContentView(v, new ConstraintLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
}

Prevention

When it happens

Trigger: Calling bottomSheet.setContentView(R.layout.my_content) on a QMUIBottomSheet.

Common situations: Porting existing Dialog code that inflates a layout resource, or copy-pasted boilerplate from AlertDialog usage.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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