Blankj/AndroidUtilCode · error · IllegalArgumentException

context is not instance of Activity.

Error message

context is not instance of Activity.

What it means

BaseDialog's constructor resolves the supplied Context to an Activity via ActivityUtils.getActivityByContext and throws IllegalArgumentException if it returns null. The dialog needs a real Activity window to attach to, so a Service/Application/wrapped context that is not backed by an Activity is rejected.

Source

Thrown at lib/base/src/main/java/com/blankj/base/dialog/BaseDialog.java:41

public abstract class BaseDialog extends Dialog {

    protected Activity mActivity;

    public abstract int bindLayout();

    public abstract void initView(BaseDialog dialog, View contentView);

    public abstract void setWindowStyle(Window window);

    public BaseDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        Activity activity = ActivityUtils.getActivityByContext(context);
        if (activity == null) {
            throw new IllegalArgumentException("context is not instance of Activity.");
        }
        mActivity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = View.inflate(mActivity, bindLayout(), null);
        setContentView(contentView);
        initView(this, contentView);
        setWindowStyle(getWindow());
    }

    @Override
    public void show() {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Pass the hosting Activity instance (e.g., MainActivity.this / requireActivity()) into the BaseDialog constructor, not getApplicationContext().
  2. If you only have a generic Context, unwrap it first: Activity a = ActivityUtils.getActivityByContext(context); and branch or fail gracefully when it is null before constructing the dialog.
  3. When inside a Fragment, obtain the Activity via getActivity()/requireActivity() and pass that.
  4. Avoid instantiating dialogs from Services/BroadcastReceivers; surface the request through an Activity instead.

Example fix

// before
new MyDialog(getApplicationContext()); // throws: not an Activity

// after
new MyDialog(MyActivity.this);
// or guard first:
Activity a = ActivityUtils.getActivityByContext(ctx);
if (a != null) new MyDialog(a);
Defensive patterns

Strategy: validation

Validate before calling

Activity a = ActivityUtils.getActivityByContext(context);
if (a == null) {
    // cannot show dialog from this context; degrade gracefully
    return;
}
BaseDialog dialog = new MyDialog(a);

Type guard

private static boolean isActivityContext(Context c) {
    return ActivityUtils.getActivityByContext(c) != null;
}

Try / catch

try {
    new MyDialog(ctx).show();
} catch (IllegalArgumentException e) {
    // ctx was not an Activity; surface via a UI context instead
}

Prevention

When it happens

Trigger: Constructing BaseDialog (or any subclass) with getApplicationContext(), a Service Context, a BroadcastReceiver/Bridge context, or any ContextWrapper that does not eventually unwrap to an Activity.

Common situations: Passing getApplicationContext() instead of the Activity to a dialog helper; creating a dialog from a non-UI component (notification, service, worker); wrapping the Activity in a ContextThemeWrapper whose base context still resolves, but using a deeper wrapper that ActivityUtils cannot unwind.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/d121b7d85fd49a7b. Report an issue: GitHub.