{"record":{"id":"8393c043b614d8df","repo":"gyf-dev/ImmersionBar","slug":"owner-must-be-attached-to-a-window","errorCode":null,"errorMessage":"Owner must be attached to a Window.","messagePattern":"Owner must be attached to a Window\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"immersionbar/src/main/java/com/gyf/immersionbar/BarConfig.java","lineNumber":119,"sourceCode":"        Resources res = context.getResources();\n        mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);\n        mSmallestWidthDp = getSmallestWidthDp(window);\n        mStatusBarHeight = getStatusBarHeight(window);\n        mStatusBarVisible = isStatusBarVisible(window);\n        mActionBarHeight = getActionBarHeight(window);\n        mNavigationBarVisible = navigationBarVisibleHint != null ? navigationBarVisibleHint : isNavigationBarVisible(window);\n        mNavigationBarHeight = getNavigationBarHeight(window, mNavigationBarVisible);\n        int navigationBarHeightIgnoringVisibility = getNavigationBarHeightIgnoringVisibility(window);\n        mNavigationBarHeightIgnoringVisibility = navigationBarHeightIgnoringVisibility > 0 ? navigationBarHeightIgnoringVisibility : (mNavigationBarVisible ? mNavigationBarHeight : 0);\n        mNavigationBarWidth = getNavigationBarWidth(window);\n        mHasNavigationBar = (mNavigationBarHeight > 0);\n        mNavigationAtBottom = computeNavigationAtBottom(window);\n    }\n\n    @NonNull\n    private static Window requireWindow(Window window) {\n        if (window == null) {\n            throw new IllegalArgumentException(\"Owner must be attached to a Window.\");\n        }\n        return window;\n    }\n\n    /**\n     * 计算导航栏是否在底部。\n     * android 11(API 30)起WindowInsets可用，优先用其实际方向判断（底部有inset即在底部，否则在侧边），\n     * 比老的“手机横屏导航栏在侧边”启发式更准；insets不可用或低版本时回退到原启发式。\n     *\n     * @param window window\n     * @return true表示导航栏在底部，false表示在侧边\n     */\n    private boolean computeNavigationAtBottom(Window window) {\n        if (Build.VERSION.SDK_INT >= Version.R) {\n            Insets navInsets = getNavigationBarInsets(window);\n            if (navInsets != null && (navInsets.bottom > 0 || navInsets.left > 0 || navInsets.right > 0)) {\n                return navInsets.bottom > 0;\n            }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/gyf-dev/ImmersionBar/blob/8cac10cd83d809eac54f42b37f92340c4a25c4ac/immersionbar/src/main/java/com/gyf/immersionbar/BarConfig.java#L101-L137","documentation":"BarConfig reads window/system-bar metrics, and requireWindow is a null-check guard on the Window passed to it. It throws IllegalArgumentException(\"Owner must be attached to a Window.\") when the supplied Window reference is null, i.e. the owner (Activity/Dialog) has not finished attaching yet.","triggerScenarios":"Constructing BarConfig with activity.getWindow() or dialog.getWindow() that returns null — typically before onCreate/onAttachedToWindow completes or after the window is destroyed.","commonSituations":"Measuring bar insets in Fragment.onCreate or a constructor instead of onViewCreated/onActivityCreated; querying config after activity finish/rotation teardown; a Dialog shown before it has a window.","solutions":["Defer BarConfig creation until the Activity window exists (after onCreate / in onResume or onViewCreated)","Null-check activity.getWindow() before constructing BarConfig and skip/retry the measurement","For dialogs, run config setup in onAttachedToWindow or after show()"],"exampleFix":"// before\nBarConfig config = new BarConfig(getActivity().getWindow()); // NPE-ish before attach\n// after\nWindow w = getActivity() != null ? getActivity().getWindow() : null;\nif (w != null) {\n    BarConfig config = new BarConfig(w);\n}","handlingStrategy":"validation","validationCode":"if (activity != null && activity.getWindow() != null) { new BarConfig(activity.getWindow()); }","typeGuard":"Window safeWindow(Activity a) { return a == null ? null : a.getWindow(); }","tryCatchPattern":"try { config = new BarConfig(window); } catch (IllegalArgumentException e) { config = null; /* defer measurement */ }","preventionTips":["Create BarConfig only after attach (onViewCreated/onResume)","For dialogs, wait for onAttachedToWindow/show()","Never measure bars from constructors or before onCreate finishes"],"tags":["android","window","lifecycle","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"8cac10cd83d809eac54f42b37f92340c4a25c4ac","analyzedAt":"2026-09-08T11:52:01.757Z","contentChangedAt":"2026-09-08T11:52:01.757Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}