{"record":{"id":"6324bf301a4d6dcc","repo":"Justson/AgentWeb","slug":"webparentlayout-context-must-be-activity-or-activi","errorCode":null,"errorMessage":"WebParentLayout context must be activity or activity sub class .","messagePattern":"WebParentLayout context must be activity or activity sub class \\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agentweb-core/src/main/java/com/just/agentweb/WebParentLayout.java","lineNumber":62,"sourceCode":"\t@IdRes\n\tprivate int mClickId = -1;\n\tprivate View mErrorView;\n\tprivate WebView mWebView;\n\tprivate FrameLayout mErrorLayout = null;\n\n\tWebParentLayout(@NonNull Context context) {\n\t\tthis(context, null);\n\t\tLogUtils.i(TAG, \"WebParentLayout\");\n\t}\n\n\tWebParentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {\n\t\tthis(context, attrs, -1);\n\t}\n\n\tWebParentLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {\n\t\tsuper(context, attrs, defStyleAttr);\n\t\tif (!(context instanceof Activity)) {\n\t\t\tthrow new IllegalArgumentException(\"WebParentLayout context must be activity or activity sub class .\");\n\t\t}\n\t\tthis.mErrorLayoutRes = R.layout.agentweb_error_page;\n\t}\n\n\tvoid bindController(AbsAgentWebUIController agentWebUIController) {\n\t\tthis.mAgentWebUIController = agentWebUIController;\n\t\tthis.mAgentWebUIController.bindWebParent(this, (Activity) getContext());\n\t}\n\n\tvoid showPageMainFrameError() {\n\t\tView container = this.mErrorLayout;\n\t\tif (container != null) {\n\t\t\tcontainer.setVisibility(View.VISIBLE);\n\t\t} else {\n\t\t\tcreateErrorLayout();\n\t\t\tcontainer = this.mErrorLayout;\n\t\t}\n\t\tView clickView = null;","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/Justson/AgentWeb/blob/8f7f6adbf01010e9b2b44638f31edf10e1ba53f7/agentweb-core/src/main/java/com/just/agentweb/WebParentLayout.java#L44-L80","documentation":"WebParentLayout is a custom FrameLayout used by AgentWeb as the root container for the WebView UI, and it requires an Activity context to work (it binds to the Activity's UI controller and error page). Its constructor throws IllegalArgumentException when the supplied Context is not an Activity or Activity subclass, because a non-activity context cannot provide the lifecycle/views it needs.","triggerScenarios":"Inflating WebParentLayout from XML or constructing `new WebParentLayout(context, attrs)` with an application context, a Service context, a themed ApplicationContext (e.g. getApplicationContext()), or passing a ContextWrapper that does not wrap an Activity.","commonSituations":"Passing getApplicationContext() instead of the Activity when building views programmatically; inflating a layout containing this view in a non-Activity context (e.g. custom View factory, RecyclerView item inflation with application theme); using the layout inside a Dialog with application context.","solutions":["Pass the current Activity (or its subclass) as the Context: `new WebParentLayout(activity, null)`","If you only have a Context, unwrap/cast it: `(Activity) context` or unwrap ContextWrapper until the Activity is found","If inflating from XML, do it with an Activity-inflater: `activity.getLayoutInflater()` or `LayoutInflater.from(activity)`","Use AgentWeb's own APIs (AgentWeb.with(activity)...) so it creates WebParentLayout with the correct context internally"],"exampleFix":"// before\nWebParentLayout layout = new WebParentLayout(getApplicationContext(), null); // throws\n// after\nWebParentLayout layout = new WebParentLayout(MyActivity.this, null);","handlingStrategy":"validation","validationCode":"public static boolean canCreateWebParentLayout(Context c) {\n    while (c instanceof ContextWrapper && !(c instanceof Activity)) {\n        c = ((ContextWrapper) c).getBaseContext();\n    }\n    return c instanceof Activity;\n}\n// call before constructing; if false, pass the Activity instead","typeGuard":"public static Activity asActivity(Context c) {\n    while (c instanceof ContextWrapper && !(c instanceof Activity)) {\n        c = ((ContextWrapper) c).getBaseContext();\n    }\n    return (c instanceof Activity) ? (Activity) c : null;\n}","tryCatchPattern":"try {\n    WebParentLayout layout = new WebParentLayout(context, null);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"WebParentLayout context must be activity\")) {\n        Activity activity = asActivity(context);\n        if (activity != null) {\n            WebParentLayout layout = new WebParentLayout(activity, null);\n        }\n    }\n}","preventionTips":["Always use Activity context (this/MyActivity.this) for views that bind to an Activity lifecycle","Never pass getApplicationContext() when inflating or constructing custom views","Unwrap ContextWrapper chains before assuming a context type","Prefer AgentWeb.with(activity) builder APIs so the library supplies the correct context","In XML inflation, ensure the inflater was created from an Activity, not the application"],"tags":["android","context","activity","view-inflation"],"backgroundTag":"invalid-argument-value","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"}