{"record":{"id":"0f5f5f5186e6ec55","repo":"yuliskov/SmartTube","slug":"you-must-pass-either-activity-fragment-or-support","errorCode":null,"errorMessage":"You must pass either Activity, Fragment or SupportFragment","messagePattern":"You must pass either Activity, Fragment or SupportFragment","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"filepicker-lib/src/main/java/arte/programar/materialfile/MaterialFilePicker.java","lineNumber":46,"sourceCode":"\n    private Pattern mFileFilter;\n    private Boolean mDirectoriesFilter = false;\n    private String mRootPath;\n    private String mCurrentPath;\n    private Boolean mShowHidden = false;\n    private Boolean mCloseable = true;\n    private CharSequence mTitle;\n\n    public MaterialFilePicker() {\n    }\n\n    /**\n     * Specifies activity, which will be used to\n     * start file picker\n     */\n    public MaterialFilePicker withActivity(Activity activity) {\n        if (mSupportFragment != null || mFragment != null) {\n            throw new RuntimeException(\"You must pass either Activity, Fragment or SupportFragment\");\n        }\n\n        mActivity = activity;\n        return this;\n    }\n\n    /**\n     * Specifies fragment, which will be used to\n     * start file picker\n     */\n    public MaterialFilePicker withFragment(Fragment fragment) {\n        if (mSupportFragment != null || mActivity != null) {\n            throw new RuntimeException(\"You must pass either Activity, Fragment or SupportFragment\");\n        }\n\n        mFragment = fragment;\n        return this;\n    }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/yuliskov/SmartTube/blob/3de8d90593e0166f012ca18cc3c7ba45bfd68ac4/filepicker-lib/src/main/java/arte/programar/materialfile/MaterialFilePicker.java#L28-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Keep exactly one with* context call per MaterialFilePicker instance","Instantiate a fresh MaterialFilePicker for each launch instead of reusing a field","Search the builder chain for duplicate with* context calls and delete the stale one"],"exampleFix":"// before\nnew MaterialFilePicker()\n        .withActivity(activity)\n        .withSupportFragment(fragment) // throws: second context\n        .show();\n\n// after\nnew MaterialFilePicker()\n        .withSupportFragment(fragment)\n        .show();","handlingStrategy":"validation","validationCode":"MaterialFilePicker picker = new MaterialFilePicker();\nif (fragment != null) {\n    picker.withSupportFragment(fragment);\n} else {\n    picker.withActivity(activity);\n}\n// exactly one context call on any code path\npicker.show();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create a fresh MaterialFilePicker per launch; never share builder instances","Encapsulate the context choice in one if/else so both branches cannot run","After migrating a screen between Activity and Fragment, grep for leftover with* calls"],"tags":["android","filepicker","builder","fragment","api-misuse"],"backgroundTag":"builder-conflicting-context","analyzedSha":"3de8d90593e0166f012ca18cc3c7ba45bfd68ac4","analyzedAt":"2026-08-22T09:19:26.383Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}