{"record":{"id":"d53fd105738fddad","repo":"Tencent/QMUI_Android","slug":"parameter-listener-must-not-be-null","errorCode":null,"errorMessage":"Parameter:listener must not be null","messagePattern":"Parameter:listener must not be null","errorType":"validation","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"qmui/src/main/java/com/qmuiteam/qmui/util/QMUIKeyboardHelper.java","lineNumber":155,"sourceCode":"        });\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\n                    @Override\n                    public void onGlobalLayout() {\n                        activityRoot.getWindowVisibleDisplayFrame(r);\n","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/Tencent/QMUI_Android/blob/026e7d486677d6593a96689cd590ec3561176717/qmui/src/main/java/com/qmuiteam/qmui/util/QMUIKeyboardHelper.java#L137-L173","documentation":"The same null-check pattern as the activity parameter: setVisibilityEventListener also requires a non-null KeyboardVisibilityEventListener to receive callbacks. A null listener is rejected with this NullPointerException before the global layout listener is installed.","triggerScenarios":"Calling QMUIKeyboardHelper.setVisibilityEventListener(activity, null), or passing a field/callback reference that is still null at call time.","commonSituations":"Callback fields initialized asynchronously (e.g. set later by DI or a presenter) before the helper call, or refactoring that removed listener creation but kept the call.","solutions":["Pass a concrete listener instance; create it inline if the callback is optional.","Null-check the listener and skip registration when it isn't ready yet.","Ensure the listener field is initialized before this call (constructor or onCreate).","If the callback truly is optional, wrap it in a no-op implementation instead of null."],"exampleFix":"// before\nQMUIKeyboardHelper.setVisibilityEventListener(activity, mListener); // mListener not yet set\n// after\nif (activity != null && mListener != null) {\n    QMUIKeyboardHelper.setVisibilityEventListener(activity, mListener);\n}","handlingStrategy":"validation","validationCode":"if (activity != null && listener != null) {\n    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);\n}","typeGuard":"boolean isValidListener(KeyboardVisibilityEventListener l) { return l != null; }","tryCatchPattern":"try {\n    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);\n} catch (NullPointerException e) {\n    Log.w(TAG, \"listener not ready for keyboard registration\", e);\n}","preventionTips":["Initialize callback fields before any registration call (constructor/onCreate).","Pass an inline listener literal rather than a possibly-unassigned field.","Use Optional/no-op default implementations when the callback is optional.","Run lint/annotations (@NonNull/@Nullable) to catch null listener arguments at compile time."],"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"}