Justson/AgentWeb · error · IllegalArgumentException

WebParentLayout context must be activity or activity sub…

Error message

WebParentLayout context must be activity or activity sub class .

What it means

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.

Solutions

  1. Pass the current Activity (or its subclass) as the Context: `new WebParentLayout(activity, null)`
  2. If you only have a Context, unwrap/cast it: `(Activity) context` or unwrap ContextWrapper until the Activity is found
  3. If inflating from XML, do it with an Activity-inflater: `activity.getLayoutInflater()` or `LayoutInflater.from(activity)`
  4. Use AgentWeb's own APIs (AgentWeb.with(activity)...) so it creates WebParentLayout with the correct context internally

Example fix

// before
WebParentLayout layout = new WebParentLayout(getApplicationContext(), null); // throws
// after
WebParentLayout layout = new WebParentLayout(MyActivity.this, null);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean canCreateWebParentLayout(Context c) {
    while (c instanceof ContextWrapper && !(c instanceof Activity)) {
        c = ((ContextWrapper) c).getBaseContext();
    }
    return c instanceof Activity;
}
// call before constructing; if false, pass the Activity instead

Type guard

public static Activity asActivity(Context c) {
    while (c instanceof ContextWrapper && !(c instanceof Activity)) {
        c = ((ContextWrapper) c).getBaseContext();
    }
    return (c instanceof Activity) ? (Activity) c : null;
}

Try / catch

try {
    WebParentLayout layout = new WebParentLayout(context, null);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().contains("WebParentLayout context must be activity")) {
        Activity activity = asActivity(context);
        if (activity != null) {
            WebParentLayout layout = new WebParentLayout(activity, null);
        }
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at agentweb-core/src/main/java/com/just/agentweb/WebParentLayout.java:62

	@IdRes
	private int mClickId = -1;
	private View mErrorView;
	private WebView mWebView;
	private FrameLayout mErrorLayout = null;

	WebParentLayout(@NonNull Context context) {
		this(context, null);
		LogUtils.i(TAG, "WebParentLayout");
	}

	WebParentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
		this(context, attrs, -1);
	}

	WebParentLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		if (!(context instanceof Activity)) {
			throw new IllegalArgumentException("WebParentLayout context must be activity or activity sub class .");
		}
		this.mErrorLayoutRes = R.layout.agentweb_error_page;
	}

	void bindController(AbsAgentWebUIController agentWebUIController) {
		this.mAgentWebUIController = agentWebUIController;
		this.mAgentWebUIController.bindWebParent(this, (Activity) getContext());
	}

	void showPageMainFrameError() {
		View container = this.mErrorLayout;
		if (container != null) {
			container.setVisibility(View.VISIBLE);
		} else {
			createErrorLayout();
			container = this.mErrorLayout;
		}
		View clickView = null;

View on GitHub (pinned to 8f7f6adbf0)