{"record":{"id":"d121b7d85fd49a7b","repo":"Blankj/AndroidUtilCode","slug":"context-is-not-instance-of-activity","errorCode":null,"errorMessage":"context is not instance of Activity.","messagePattern":"context is not instance of Activity\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"lib/base/src/main/java/com/blankj/base/dialog/BaseDialog.java","lineNumber":41,"sourceCode":"public abstract class BaseDialog extends Dialog {\n\n    protected Activity mActivity;\n\n    public abstract int bindLayout();\n\n    public abstract void initView(BaseDialog dialog, View contentView);\n\n    public abstract void setWindowStyle(Window window);\n\n    public BaseDialog(@NonNull Context context) {\n        this(context, 0);\n    }\n\n    public BaseDialog(@NonNull Context context, int themeResId) {\n        super(context, themeResId);\n        Activity activity = ActivityUtils.getActivityByContext(context);\n        if (activity == null) {\n            throw new IllegalArgumentException(\"context is not instance of Activity.\");\n        }\n        mActivity = activity;\n    }\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        View contentView = View.inflate(mActivity, bindLayout(), null);\n        setContentView(contentView);\n        initView(this, contentView);\n        setWindowStyle(getWindow());\n    }\n\n    @Override\n    public void show() {\n        ThreadUtils.runOnUiThread(new Runnable() {\n            @Override\n            public void run() {","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/base/src/main/java/com/blankj/base/dialog/BaseDialog.java#L23-L59","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the hosting Activity instance (e.g., MainActivity.this / requireActivity()) into the BaseDialog constructor, not getApplicationContext().","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.","When inside a Fragment, obtain the Activity via getActivity()/requireActivity() and pass that.","Avoid instantiating dialogs from Services/BroadcastReceivers; surface the request through an Activity instead."],"exampleFix":"// before\nnew MyDialog(getApplicationContext()); // throws: not an Activity\n\n// after\nnew MyDialog(MyActivity.this);\n// or guard first:\nActivity a = ActivityUtils.getActivityByContext(ctx);\nif (a != null) new MyDialog(a);","handlingStrategy":"validation","validationCode":"Activity a = ActivityUtils.getActivityByContext(context);\nif (a == null) {\n    // cannot show dialog from this context; degrade gracefully\n    return;\n}\nBaseDialog dialog = new MyDialog(a);","typeGuard":"private static boolean isActivityContext(Context c) {\n    return ActivityUtils.getActivityByContext(c) != null;\n}","tryCatchPattern":"try {\n    new MyDialog(ctx).show();\n} catch (IllegalArgumentException e) {\n    // ctx was not an Activity; surface via a UI context instead\n}","preventionTips":["Always pass the Activity instance to dialogs, never getApplicationContext().","Unwrap ContextWrapper chains via ActivityUtils.getActivityByContext before constructing a dialog.","Never construct dialogs from Services/BroadcastReceivers."],"tags":["android","dialog","context","activity"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}