Justson/AgentWeb · error · NullPointerException

activity can not be null .

Error message

activity can not be null .

What it means

AgentWeb.with(Activity) throws a NullPointerException when the Activity argument is null. The library requires a live Activity to attach the WebView and hold context, so it fails fast instead of crashing later deep inside AgentWeb's view setup. This is an explicit guard in AgentBuilder construction, distinct from any annotation-based null check.

Solutions

  1. Ensure the Activity is fully created (onCreate finished) and non-null before calling AgentWeb.with(activity)
  2. If inside a Fragment, prefer AgentWeb.with(fragment) or check fragment.getActivity() != null first
  3. If the Activity may be destroyed, re-acquire it from the current context rather than caching it
  4. Wrap the call site in a null check and skip/defer AgentWeb initialization when null

Example fix

// before
Activity act = getActivity(); // may be null when detached
AgentWeb mAgentWeb = AgentWeb.with(act)...;
// after
Activity act = getActivity();
if (act != null && !act.isFinishing()) {
    AgentWeb mAgentWeb = AgentWeb.with(act)...;
}
Defensive patterns

Strategy: validation

Validate before calling

if (activity == null || activity.isFinishing()) { throw new IllegalArgumentException("AgentWeb requires a live Activity"); }

Type guard

boolean canInitAgentWeb(Activity a) { return a != null && !a.isFinishing() && !a.isDestroyed(); }

Try / catch

try { web = AgentWeb.with(activity)...; } catch (NullPointerException e) { Log.e(TAG, "activity null for AgentWeb", e); }

Prevention

When it happens

Trigger: Calling AgentWeb.with((Activity) null) directly, or passing an Activity variable that was never assigned (e.g. a field read before onCreate, or a weakly-held Activity that has been GC'd/null-cleared).

Common situations: Passing getActivity() from a detached Fragment (returns null after detach), using an Activity reference captured before the lifecycle created it, refactoring code so the Activity is lazily resolved and resolves to null, or calling with() from an Application context.

Related errors


AI-assisted analysis of Justson/AgentWeb@8f7f6adbf0 (2026-09-11). Data as JSON: /api/errors/b638b524fd0e22d9. Report an issue: GitHub.

Appendix: source

Thrown at agentweb-core/src/main/java/com/just/agentweb/AgentWeb.java:250

            this.mJsAccessEntrace = mJsAccessEntrace = JsAccessEntraceImpl.getInstance(mWebCreator.getWebView());
        }
        return mJsAccessEntrace;
    }


    public AgentWeb clearWebCache() {
        if (this.getWebCreator().getWebView() != null) {
            AgentWebUtils.clearWebViewAllCache(mActivity, this.getWebCreator().getWebView());
        } else {
            AgentWebUtils.clearWebViewAllCache(mActivity);
        }
        return this;
    }


    public static AgentBuilder with(@NonNull Activity activity) {
        if (activity == null) {
            throw new NullPointerException("activity can not be null .");
        }
        return new AgentBuilder(activity);
    }

    public static AgentBuilder with(@NonNull Fragment fragment) {
        Activity mActivity = null;
        if ((mActivity = fragment.getActivity()) == null) {
            throw new NullPointerException("activity can not be null .");
        }
        return new AgentBuilder(mActivity, fragment);
    }

    public boolean handleKeyEvent(int keyCode, KeyEvent keyEvent) {
        if (mIEventHandler == null) {
            mIEventHandler = EventHandlerImpl.getInstantce(mWebCreator.getWebView(), getInterceptor());
        }
        return mIEventHandler.onKeyDown(keyCode, keyEvent);
    }

View on GitHub (pinned to 8f7f6adbf0)