{"record":{"id":"8e98c29c662dbc24","repo":"Tencent/QMUI_Android","slug":"parameter-activity-must-not-be-null","errorCode":null,"errorMessage":"Parameter:activity must not be null","messagePattern":"Parameter:activity must not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"qmui/src/main/java/com/qmuiteam/qmui/util/QMUIKeyboardHelper.java","lineNumber":151,"sourceCode":"                }\n                QMUIViewHelper.getOrCreateOffsetHelper(view).setTopAndBottomOffset(-height / 2);\n                return insets;\n            }\n        });\n    }\n\n    /**\n     * Set keyboard visibility change event listener.\n     *\n     * @param activity Activity\n     * @param listener KeyboardVisibilityEventListener\n     */\n    @SuppressWarnings(\"deprecation\")\n    public static void setVisibilityEventListener(final Activity activity,\n                                                  final KeyboardVisibilityEventListener listener) {\n\n        if (activity == null) {\n            throw new NullPointerException(\"Parameter:activity must not be null\");\n        }\n\n        if (listener == null) {\n            throw new NullPointerException(\"Parameter:listener must not be null\");\n        }\n\n        final View activityRoot = QMUIViewHelper.getActivityRoot(activity);\n\n        final ViewTreeObserver.OnGlobalLayoutListener layoutListener =\n                new ViewTreeObserver.OnGlobalLayoutListener() {\n\n                    private final Rect r = new Rect();\n\n                    private final int visibleThreshold = Math.round(\n                            QMUIDisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));\n\n                    private boolean wasOpened = false;\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/Tencent/QMUI_Android/blob/026e7d486677d6593a96689cd590ec3561176717/qmui/src/main/java/com/qmuiteam/qmui/util/QMUIKeyboardHelper.java#L133-L169","documentation":"QMUIKeyboardHelper.setVisibilityEventListener requires a non-null Activity to install a keyboard-visibility listener on the activity root view. Passing null causes an explicit NullPointerException with this message, thrown before any work is done.","triggerScenarios":"Calling QMUIKeyboardHelper.setVisibilityEventListener(null, listener) or passing an expression that evaluates to null (e.g. getActivity() returning null after the Fragment is detached).","commonSituations":"Fragment-based screens where getActivity() is null after onDetach(), or async callbacks that fire after the activity is destroyed, or wiring the helper in Application scope where no activity exists yet.","solutions":["Check activity != null before calling; if null, skip registration.","Register the listener from the Fragment only while isAdded() is true.","Hold the activity in a field captured at onCreate/onAttach instead of calling getActivity() in a late callback.","Use requireActivity() in Fragments so an IllegalStateException surfaces at the right place rather than inside the helper."],"exampleFix":"// before\nQMUIKeyboardHelper.setVisibilityEventListener(getActivity(), listener); // NPE if detached\n// after\nActivity activity = getActivity();\nif (activity != null) {\n    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);\n}","handlingStrategy":"validation","validationCode":"if (activity == null || activity.isFinishing()) return;\nQMUIKeyboardHelper.setVisibilityEventListener(activity, listener);","typeGuard":null,"tryCatchPattern":"try {\n    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);\n} catch (NullPointerException e) {\n    Log.w(TAG, \"activity not available for keyboard listener\", e);\n}","preventionTips":["Guard getActivity() results with null checks in Fragments.","Register keyboard listeners only in onStart/onResume; unregister in the symmetric lifecycle method.","Avoid registering from async callbacks that may run after detach.","Use requireActivity()/captured-onCreate activity references instead of late lookups."],"tags":["android","qmui","null-argument","keyboard"],"backgroundTag":"null-argument","analyzedSha":"026e7d486677d6593a96689cd590ec3561176717","analyzedAt":"2026-09-06T13:32:24.816Z","contentChangedAt":"2026-09-06T13:32:24.816Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}