yuliskov/SmartTube · error · RuntimeException

You must pass Activity/Fragment by calling withActivity/with

Error message

You must pass Activity/Fragment by calling withActivity/withFragment/withSupportFragment and withActivityResultApi method

What it means

getActivity() is called internally when the builder builds its launch intent and starts the picker. If no host was ever set (mActivity, mFragment and mSupportFragment all null), it throws this RuntimeException. The message also mentions withActivityResultApi, but that API is commented out in this version of the library — the actual fix is always one of withActivity/withFragment/withSupportFragment.

Source

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

        }

        if (mCurrentPath != null) {
            intent.putExtra(FilePickerActivity.ARG_CURRENT_FILE, new File(mCurrentPath));
        }

        if (mTitle != null) {
            intent.putExtra(FilePickerActivity.ARG_TITLE, mTitle);
        }

        return intent;
    }

    private Activity getActivity() {
        Activity activity = null;

        //if (mActivity == null && mFragment == null && mSupportFragment == null && mStartForResultFiles == null) {
        if (mActivity == null && mFragment == null && mSupportFragment == null) {
            throw new RuntimeException(
                    "You must pass Activity/Fragment by calling " +
                            "withActivity/withFragment/withSupportFragment and " +
                            "withActivityResultApi method"
            );
        }

        if (mActivity != null) {
            activity = mActivity;
        } else if (mFragment != null) {
            activity = mFragment.getActivity();
        } else if (mSupportFragment != null) {
            activity = mSupportFragment.getActivity();
        }

        return activity;
    }

    private CompositeFilter getFilter() {

View on GitHub (pinned to 3de8d90593)

Solutions

  1. Add withActivity(activity) (or withFragment/withSupportFragment for fragments) to the chain before start().
  2. Ignore the withActivityResultApi mention in the message — that method is commented out in this build; only the three with* host setters exist.
  3. Wrap the picker in one factory method that enforces host + requestCode so a host-less chain cannot be written.
  4. Search your code for 'new MaterialFilePicker' and verify every chain contains a host call.

Example fix

// before
new MaterialFilePicker()
    .withRequestCode(1000)
    .withHiddenFiles(true)
    .start(); // throws: no host set

// after
new MaterialFilePicker()
    .withActivity(this)
    .withRequestCode(1000)
    .withHiddenFiles(true)
    .start();
Defensive patterns

Strategy: validation

Validate before calling

public static void openPicker(Activity host, int requestCode) {
    if (host == null) {
        throw new IllegalArgumentException("host required");
    }
    new MaterialFilePicker()
            .withActivity(host) // host always set before start()
            .withRequestCode(requestCode)
            .start();
}

Prevention

When it happens

Trigger: new MaterialFilePicker().withRequestCode(1).start() (or any other chain) that never calls a with* host method, so the internal getActivity() finds all three host fields null.

Common situations: Refactoring removes the withActivity line and nothing fails at compile time because the builder API is fully chainable; copying a partial builder chain from docs; developers coming from the ActivityResultLauncher fork misled by the stale message into looking for withActivityResultApi.

Related errors


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