Tencent/QMUI_Android · error · NullPointerException

Parameter:activity must not be null

Error message

Parameter:activity must not be null

What it means

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.

Source

Thrown at qmui/src/main/java/com/qmuiteam/qmui/util/QMUIKeyboardHelper.java:151

                }
                QMUIViewHelper.getOrCreateOffsetHelper(view).setTopAndBottomOffset(-height / 2);
                return insets;
            }
        });
    }

    /**
     * Set keyboard visibility change event listener.
     *
     * @param activity Activity
     * @param listener KeyboardVisibilityEventListener
     */
    @SuppressWarnings("deprecation")
    public static void setVisibilityEventListener(final Activity activity,
                                                  final KeyboardVisibilityEventListener listener) {

        if (activity == null) {
            throw new NullPointerException("Parameter:activity must not be null");
        }

        if (listener == null) {
            throw new NullPointerException("Parameter:listener must not be null");
        }

        final View activityRoot = QMUIViewHelper.getActivityRoot(activity);

        final ViewTreeObserver.OnGlobalLayoutListener layoutListener =
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    private final Rect r = new Rect();

                    private final int visibleThreshold = Math.round(
                            QMUIDisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));

                    private boolean wasOpened = false;

View on GitHub (pinned to 026e7d4866)

Solutions

  1. Check activity != null before calling; if null, skip registration.
  2. Register the listener from the Fragment only while isAdded() is true.
  3. Hold the activity in a field captured at onCreate/onAttach instead of calling getActivity() in a late callback.
  4. Use requireActivity() in Fragments so an IllegalStateException surfaces at the right place rather than inside the helper.

Example fix

// before
QMUIKeyboardHelper.setVisibilityEventListener(getActivity(), listener); // NPE if detached
// after
Activity activity = getActivity();
if (activity != null) {
    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);
}
Defensive patterns

Strategy: validation

Validate before calling

if (activity == null || activity.isFinishing()) return;
QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);

Try / catch

try {
    QMUIKeyboardHelper.setVisibilityEventListener(activity, listener);
} catch (NullPointerException e) {
    Log.w(TAG, "activity not available for keyboard listener", e);
}

Prevention

When it happens

Trigger: Calling QMUIKeyboardHelper.setVisibilityEventListener(null, listener) or passing an expression that evaluates to null (e.g. getActivity() returning null after the Fragment is detached).

Common situations: 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.

Related errors


AI-assisted analysis of Tencent/QMUI_Android@026e7d4866 (2026-09-06). Data as JSON: /api/errors/8e98c29c662dbc24. Report an issue: GitHub.