{"record":{"id":"b638b524fd0e22d9","repo":"Justson/AgentWeb","slug":"activity-can-not-be-null","errorCode":null,"errorMessage":"activity can not be null .","messagePattern":"activity can not be null \\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"agentweb-core/src/main/java/com/just/agentweb/AgentWeb.java","lineNumber":250,"sourceCode":"            this.mJsAccessEntrace = mJsAccessEntrace = JsAccessEntraceImpl.getInstance(mWebCreator.getWebView());\r\n        }\r\n        return mJsAccessEntrace;\r\n    }\r\n\r\n\r\n    public AgentWeb clearWebCache() {\r\n        if (this.getWebCreator().getWebView() != null) {\r\n            AgentWebUtils.clearWebViewAllCache(mActivity, this.getWebCreator().getWebView());\r\n        } else {\r\n            AgentWebUtils.clearWebViewAllCache(mActivity);\r\n        }\r\n        return this;\r\n    }\r\n\r\n\r\n    public static AgentBuilder with(@NonNull Activity activity) {\r\n        if (activity == null) {\r\n            throw new NullPointerException(\"activity can not be null .\");\r\n        }\r\n        return new AgentBuilder(activity);\r\n    }\r\n\r\n    public static AgentBuilder with(@NonNull Fragment fragment) {\r\n        Activity mActivity = null;\r\n        if ((mActivity = fragment.getActivity()) == null) {\r\n            throw new NullPointerException(\"activity can not be null .\");\r\n        }\r\n        return new AgentBuilder(mActivity, fragment);\r\n    }\r\n\r\n    public boolean handleKeyEvent(int keyCode, KeyEvent keyEvent) {\r\n        if (mIEventHandler == null) {\r\n            mIEventHandler = EventHandlerImpl.getInstantce(mWebCreator.getWebView(), getInterceptor());\r\n        }\r\n        return mIEventHandler.onKeyDown(keyCode, keyEvent);\r\n    }\r","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/Justson/AgentWeb/blob/8f7f6adbf01010e9b2b44638f31edf10e1ba53f7/agentweb-core/src/main/java/com/just/agentweb/AgentWeb.java#L232-L268","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the Activity is fully created (onCreate finished) and non-null before calling AgentWeb.with(activity)","If inside a Fragment, prefer AgentWeb.with(fragment) or check fragment.getActivity() != null first","If the Activity may be destroyed, re-acquire it from the current context rather than caching it","Wrap the call site in a null check and skip/defer AgentWeb initialization when null"],"exampleFix":"// before\nActivity act = getActivity(); // may be null when detached\nAgentWeb mAgentWeb = AgentWeb.with(act)...;\n// after\nActivity act = getActivity();\nif (act != null && !act.isFinishing()) {\n    AgentWeb mAgentWeb = AgentWeb.with(act)...;\n}","handlingStrategy":"validation","validationCode":"if (activity == null || activity.isFinishing()) { throw new IllegalArgumentException(\"AgentWeb requires a live Activity\"); }","typeGuard":"boolean canInitAgentWeb(Activity a) { return a != null && !a.isFinishing() && !a.isDestroyed(); }","tryCatchPattern":"try { web = AgentWeb.with(activity)...; } catch (NullPointerException e) { Log.e(TAG, \"activity null for AgentWeb\", e); }","preventionTips":["Never cache Activity references across lifecycle","Initialize AgentWeb in onCreate/onViewCreated, not constructors","For Fragments check isAdded() first","Pass the hosting Activity, never application context"],"tags":["android","null-pointer","agentweb","activity-lifecycle"],"backgroundTag":"null-argument","analyzedSha":"8f7f6adbf01010e9b2b44638f31edf10e1ba53f7","analyzedAt":"2026-09-11T10:05:24.337Z","contentChangedAt":"2026-09-11T10:05:24.337Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}