yuliskov/SmartTube · error · RuntimeException

You must pass either Activity, Fragment or SupportFragment

Error message

You must pass either Activity, Fragment or SupportFragment

What it means

MaterialFilePicker is a one-context builder: each of withActivity, withFragment and withSupportFragment throws RuntimeException if a different context was already set on the same builder instance. The message text reads like a 'nothing was passed' error, but the code shows the opposite trigger — a second context was passed after a first one.

Source

Thrown at filepicker-lib/src/main/java/arte/programar/materialfile/MaterialFilePicker.java:46

    private Pattern mFileFilter;
    private Boolean mDirectoriesFilter = false;
    private String mRootPath;
    private String mCurrentPath;
    private Boolean mShowHidden = false;
    private Boolean mCloseable = true;
    private CharSequence mTitle;

    public MaterialFilePicker() {
    }

    /**
     * Specifies activity, which will be used to
     * start file picker
     */
    public MaterialFilePicker withActivity(Activity activity) {
        if (mSupportFragment != null || mFragment != null) {
            throw new RuntimeException("You must pass either Activity, Fragment or SupportFragment");
        }

        mActivity = activity;
        return this;
    }

    /**
     * Specifies fragment, which will be used to
     * start file picker
     */
    public MaterialFilePicker withFragment(Fragment fragment) {
        if (mSupportFragment != null || mActivity != null) {
            throw new RuntimeException("You must pass either Activity, Fragment or SupportFragment");
        }

        mFragment = fragment;
        return this;
    }

View on GitHub (pinned to 3de8d90593)

Solutions

  1. Keep exactly one with* context call per MaterialFilePicker instance
  2. Instantiate a fresh MaterialFilePicker for each launch instead of reusing a field
  3. Search the builder chain for duplicate with* context calls and delete the stale one

Example fix

// before
new MaterialFilePicker()
        .withActivity(activity)
        .withSupportFragment(fragment) // throws: second context
        .show();

// after
new MaterialFilePicker()
        .withSupportFragment(fragment)
        .show();
Defensive patterns

Strategy: validation

Validate before calling

MaterialFilePicker picker = new MaterialFilePicker();
if (fragment != null) {
    picker.withSupportFragment(fragment);
} else {
    picker.withActivity(activity);
}
// exactly one context call on any code path
picker.show();

Prevention

When it happens

Trigger: Chaining two context calls on one builder, e.g. withActivity(activity).withSupportFragment(fragment); leaving a stale withSupportFragment in copied code after switching a screen from Fragment to Activity; reusing a shared builder field across launches.

Common situations: Migrating an Activity-based screen to fragments and leaving the old call in place; helper methods that apply a default context before caller code adds its own.

Related errors


AI-assisted analysis of yuliskov/SmartTube@3de8d90593 (2026-08-22). Data as JSON: /api/errors/0f5f5f5186e6ec55. Report an issue: GitHub.