{"record":{"id":"4d86919da8b0aae0","repo":"AvaloniaUI/Avalonia","slug":"context-inputmethodservice-is-expected-to-be-not-n","errorCode":null,"errorMessage":"Context.InputMethodService is expected to be not null.","messagePattern":"Context\\.InputMethodService is expected to be not null\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs","lineNumber":51,"sourceCode":"        ActionSend = 0x00000004,\n        ActionNext = 0x00000005,\n        ActionDone = 0x00000006,\n        ActionPrevious = 0x00000007,\n    }\n\n    internal class AndroidInputMethod<TView> : ITextInputMethodImpl, IAndroidInputMethod\n        where TView : View, IInitEditorInfo\n    {\n        private readonly TView _host;\n        private readonly InputMethodManager _imm;\n        private TextInputMethodClient? _client;\n        private AvaloniaInputConnection? _inputConnection;\n\n        public AndroidInputMethod(TView host)\n        {\n            _host = host;\n            _imm = host.Context?.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>()\n                   ?? throw new InvalidOperationException(\"Context.InputMethodService is expected to be not null.\");\n\n            _host.Focusable = true;\n            _host.FocusableInTouchMode = true;\n        }\n\n        public View View => _host;\n\n        [MemberNotNullWhen(true, nameof(Client))]\n        [MemberNotNullWhen(true, nameof(_client))]\n        public bool IsActive => Client != null;\n\n        public TextInputMethodClient? Client => _client;\n\n        public InputMethodManager IMM => _imm;\n\n        public void Reset()\n        {\n","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs#L33-L69","documentation":"AndroidInputMethod obtains the system InputMethodManager via Context.GetSystemService(InputMethodService). It throws InvalidOperationException if that returns null — meaning the host View's Context is null or the platform service is unavailable (rare on real devices, more common in test/instrumentation contexts). Without IMM the IME cannot be shown.","triggerScenarios":"Constructing AndroidInputMethod<TView> when host.Context is null (view not yet attached to a context) or GetSystemService(Context.InputMethodService) returns null. Common in headless unit tests of the input method or constructing before the view has a Context.","commonSituations":"Instantiating the input method in a unit test without a real Android Context; view not attached (Context null) at construction; stripped Android image without IME service; instrumentation host with no InputMethodService.","solutions":["Construct AndroidInputMethod only after the host View has a valid Context (post-attached).","In tests, inject a mock Context whose GetSystemService returns a mock InputMethodManager.","Guard the constructor: defer IMM resolution until first Show/Reset.","Ensure the target device/emulator actually has an IME (config.image input methods)."],"exampleFix":"// before\n_imm = host.Context?.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>()\n       ?? throw new InvalidOperationException(\"Context.InputMethodService is expected to be not null.\");\n\n// after (defer until context is available)\npublic InputMethodManager Imm => _imm ??= _host.Context?.GetSystemService(Context.InputMethodService)\n    .JavaCast<InputMethodManager>()\n    ?? throw new InvalidOperationException(\n        \"Could not obtain InputMethodManager; the host view has no Context or the IME service is unavailable.\");","handlingStrategy":"validation","validationCode":"// construct the input method only once the host view has a context\nif (host.Context is null) return; // defer until attached","typeGuard":"static bool HasImm(View v) => v.Context?.GetSystemService(Context.InputMethodService) is not null;","tryCatchPattern":"// construct lazily; resolve IMM on first Show so Context is available.","preventionTips":["Build the input method after the view is attached (Context non-null).","Inject a mock InputMethodManager in tests.","Defer IMM resolution to first use."],"tags":["android","ime","input-method","context","text-input","platform"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}