gyf-dev/ImmersionBar · error · IllegalArgumentException
Fragment请实现SimpleImmersionOwner接口
Error message
Fragment请实现SimpleImmersionOwner接口
What it means
SimpleImmersionProxy is a convenience wrapper that requires its Fragment to implement SimpleImmersionOwner (which only requires onSetBar). The constructor performs an instanceof check and throws IllegalArgumentException when the fragment does not implement it, since there is otherwise nothing to invoke setImmersionBar() on.
Solutions
- Have the Fragment implement SimpleImmersionOwner and override onSetBar(ImmersionBar bar)
- Extend SimpleImmersionFragment from the components library
- Switch back to ImmersionProxy if the fragment already implements ImmersionOwner
Example fix
// before
public class MyFragment extends Fragment {
private final SimpleImmersionProxy proxy = new SimpleImmersionProxy(this);
}
// after
public class MyFragment extends Fragment implements SimpleImmersionOwner {
@Override public void onSetBar(ImmersionBar bar) { bar.statusBarDarkFont(true).init(); }
private final SimpleImmersionProxy proxy = new SimpleImmersionProxy(this);
} Defensive patterns
Strategy: validation
Validate before calling
if (fragment instanceof SimpleImmersionOwner) { new SimpleImmersionProxy(fragment); } Type guard
boolean supportsSimpleImmersion(Fragment f) { return f instanceof SimpleImmersionOwner; } Try / catch
try { proxy = new SimpleImmersionProxy(fragment); } catch (IllegalArgumentException e) { Log.e(TAG, "Fragment lacks SimpleImmersionOwner", e); } Prevention
- Keep one proxy flavor per fragment hierarchy; don't mix ImmersionProxy and SimpleImmersionProxy
- Implement SimpleImmersionOwner in the base Fragment
- Extend SimpleImmersionFragment
When it happens
Trigger: Calling new SimpleImmersionProxy(fragment) with a Fragment that implements ImmersionOwner or nothing at all, but not SimpleImmersionOwner.
Common situations: Mixing the two proxy flavors: fragment was set up for ImmersionProxy but the developer switched to SimpleImmersionProxy; base class implements ImmersionOwner only; forgetting to implement SimpleImmersionOwner after adding the proxy.
Related errors
- Fragment请实现ImmersionOwner接口
- Fragment must be attached to an Activity.
- Owner must be attached to a Window.
- View参数不能为空
- tag不能为空
AI-assisted analysis of gyf-dev/ImmersionBar@8cac10cd83 (2026-09-08).
Data as JSON: /api/errors/cc4f8e9d9ae661d5.
Report an issue: GitHub.
Appendix: source
Thrown at immersionbar-components/src/main/java/com/gyf/immersionbar/components/SimpleImmersionProxy.java:34
/**
* 要操作的Fragment对象
*/
private Fragment mFragment;
/**
* 沉浸式实现接口
*/
private SimpleImmersionOwner mSimpleImmersionOwner;
/**
* Fragment的view是否已经初始化完成
*/
private boolean mIsActivityCreated;
public SimpleImmersionProxy(Fragment fragment) {
this.mFragment = fragment;
if (fragment instanceof SimpleImmersionOwner) {
this.mSimpleImmersionOwner = (SimpleImmersionOwner) fragment;
} else {
throw new IllegalArgumentException("Fragment请实现SimpleImmersionOwner接口");
}
}
public void setUserVisibleHint(boolean isVisibleToUser) {
setImmersionBar();
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
mIsActivityCreated = true;
setImmersionBar();
}
public void onDestroy() {
mFragment = null;
mSimpleImmersionOwner = null;
}
public void onConfigurationChanged(Configuration newConfig) {View on GitHub (pinned to 8cac10cd83)