{"record":{"id":"c1f2397703dc1ea4","repo":"yuliskov/SmartTube","slug":"you-must-pass-activity-fragment-by-calling-withact","errorCode":null,"errorMessage":"You must pass Activity/Fragment by calling withActivity/withFragment/withSupportFragment and withActivityResultApi method","messagePattern":"You must pass Activity/Fragment by calling withActivity/withFragment/withSupportFragment and withActivityResultApi method","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"filepicker-lib/src/main/java/arte/programar/materialfile/MaterialFilePicker.java","lineNumber":193,"sourceCode":"        }\n\n        if (mCurrentPath != null) {\n            intent.putExtra(FilePickerActivity.ARG_CURRENT_FILE, new File(mCurrentPath));\n        }\n\n        if (mTitle != null) {\n            intent.putExtra(FilePickerActivity.ARG_TITLE, mTitle);\n        }\n\n        return intent;\n    }\n\n    private Activity getActivity() {\n        Activity activity = null;\n\n        //if (mActivity == null && mFragment == null && mSupportFragment == null && mStartForResultFiles == null) {\n        if (mActivity == null && mFragment == null && mSupportFragment == null) {\n            throw new RuntimeException(\n                    \"You must pass Activity/Fragment by calling \" +\n                            \"withActivity/withFragment/withSupportFragment and \" +\n                            \"withActivityResultApi method\"\n            );\n        }\n\n        if (mActivity != null) {\n            activity = mActivity;\n        } else if (mFragment != null) {\n            activity = mFragment.getActivity();\n        } else if (mSupportFragment != null) {\n            activity = mSupportFragment.getActivity();\n        }\n\n        return activity;\n    }\n\n    private CompositeFilter getFilter() {","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/yuliskov/SmartTube/blob/3de8d90593e0166f012ca18cc3c7ba45bfd68ac4/filepicker-lib/src/main/java/arte/programar/materialfile/MaterialFilePicker.java#L175-L211","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add withActivity(activity) (or withFragment/withSupportFragment for fragments) to the chain before start().","Ignore the withActivityResultApi mention in the message — that method is commented out in this build; only the three with* host setters exist.","Wrap the picker in one factory method that enforces host + requestCode so a host-less chain cannot be written.","Search your code for 'new MaterialFilePicker' and verify every chain contains a host call."],"exampleFix":"// before\nnew MaterialFilePicker()\n    .withRequestCode(1000)\n    .withHiddenFiles(true)\n    .start(); // throws: no host set\n\n// after\nnew MaterialFilePicker()\n    .withActivity(this)\n    .withRequestCode(1000)\n    .withHiddenFiles(true)\n    .start();","handlingStrategy":"validation","validationCode":"public static void openPicker(Activity host, int requestCode) {\n    if (host == null) {\n        throw new IllegalArgumentException(\"host required\");\n    }\n    new MaterialFilePicker()\n            .withActivity(host) // host always set before start()\n            .withRequestCode(requestCode)\n            .start();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Every 'new MaterialFilePicker' chain must contain one host call — enforce via a single wrapper method.","The message's withActivityResultApi hint is stale in this build; only withActivity/withFragment/withSupportFragment exist.","Code-review builder chains for a host call before start()."],"tags":["android","builder","missing-parameter","file-picker"],"backgroundTag":"missing-required-builder-parameter","analyzedSha":"3de8d90593e0166f012ca18cc3c7ba45bfd68ac4","analyzedAt":"2026-08-22T09:19:26.383Z","schemaVersion":2},"datasetVersion":"2026-08-22T14:17:55.899Z"}