gyf-dev/ImmersionBar · error · NullPointerException
message (variable, e.g. "activity is null", "fragment is…
Error message
message (variable, e.g. "activity is null", "fragment is null")
What it means
RequestManagerRetriever.checkNotNull throws NullPointerException with a caller-provided message (e.g. "activity is null", "fragment is null") when an argument to get()/supportFragmentGet() etc. is null. The library cannot attach its hidden support fragment without a live context owner, so it fails fast.
Solutions
- Ensure a non-null, started Activity or attached Fragment is passed to get()
- Unwrap ContextWrapper to verify an Activity exists before calling
- Null-check the source (getActivity()/getParentFragment()) and retry after attachment
Example fix
// before
Fragment f = retriever.get(fragment.getActivity()); // activity may be null
// after
Activity activity = fragment.getActivity();
if (activity != null) {
Fragment f = retriever.get(activity);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (activity != null) { retriever.get(activity); } Type guard
Activity unwrapActivity(Context c) { while (c instanceof ContextWrapper) { if (c instanceof Activity) return (Activity) c; c = ((ContextWrapper) c).getBaseContext(); } return null; } Try / catch
try { retriever.get(context); } catch (NullPointerException e) { Log.e(TAG, "no activity available: " + e.getMessage()); } Prevention
- Never pass Application context where an Activity is required
- Check fragment.getActivity() != null before lookups
- Avoid retriever calls from detached fragments or background threads
When it happens
Trigger: Calling the retriever's get(Context)/get(Fragment) with a null Activity, null Fragment, or a context whose unwrapped activity is null (Application context, detached fragment).
Common situations: Requesting fragment manager access from a detached Fragment; passing applicationContext where an Activity is required; lifecycle races where the fragment was detached before the lookup; manually invoking retriever code rather than the public facade.
Related errors
- Owner must be attached to a Window.
- Fragment must be attached to an Activity.
- View参数不能为空
- tag不能为空
- Fragment请实现ImmersionOwner接口
AI-assisted analysis of gyf-dev/ImmersionBar@8cac10cd83 (2026-09-08).
Data as JSON: /api/errors/ddbce46f1223280a.
Report an issue: GitHub.
Appendix: source
Thrown at immersionbar/src/main/java/com/gyf/immersionbar/RequestManagerRetriever.java:379
putPendingSupportFragment(fm, tag, fragment);
fm.beginTransaction().add(fragment, tag).commitAllowingStateLoss();
mHandler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER, new PendingFragment(fm, tag)).sendToTarget();
}
}
if (destroy) {
if (mPendingSupportRemoveFragments.get(tag) == null) {
mPendingSupportRemoveFragments.put(tag, fragment);
fm.beginTransaction().remove(fragment).commitAllowingStateLoss();
mHandler.obtainMessage(ID_REMOVE_SUPPORT_FRAGMENT_MANAGER_REMOVE, tag).sendToTarget();
}
return null;
}
return fragment;
}
private static <T> void checkNotNull(@Nullable T arg, @NonNull String message) {
if (arg == null) {
throw new NullPointerException(message);
}
}
private static class PendingFragment {
final Object fragmentManager;
final String tag;
PendingFragment(Object fragmentManager, String tag) {
this.fragmentManager = fragmentManager;
this.tag = tag;
}
}
}
View on GitHub (pinned to 8cac10cd83)