AvaloniaUI/Avalonia · error · InvalidOperationException
Context.InputMethodService is expected to be not null.
Error message
Context.InputMethodService is expected to be not null.
What it means
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.
Source
Thrown at src/Android/Avalonia.Android/Platform/Input/AndroidInputMethod.cs:51
ActionSend = 0x00000004,
ActionNext = 0x00000005,
ActionDone = 0x00000006,
ActionPrevious = 0x00000007,
}
internal class AndroidInputMethod<TView> : ITextInputMethodImpl, IAndroidInputMethod
where TView : View, IInitEditorInfo
{
private readonly TView _host;
private readonly InputMethodManager _imm;
private TextInputMethodClient? _client;
private AvaloniaInputConnection? _inputConnection;
public AndroidInputMethod(TView host)
{
_host = host;
_imm = host.Context?.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>()
?? throw new InvalidOperationException("Context.InputMethodService is expected to be not null.");
_host.Focusable = true;
_host.FocusableInTouchMode = true;
}
public View View => _host;
[MemberNotNullWhen(true, nameof(Client))]
[MemberNotNullWhen(true, nameof(_client))]
public bool IsActive => Client != null;
public TextInputMethodClient? Client => _client;
public InputMethodManager IMM => _imm;
public void Reset()
{
View on GitHub (pinned to 11c5427268)
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).
Example fix
// before
_imm = host.Context?.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>()
?? throw new InvalidOperationException("Context.InputMethodService is expected to be not null.");
// after (defer until context is available)
public InputMethodManager Imm => _imm ??= _host.Context?.GetSystemService(Context.InputMethodService)
.JavaCast<InputMethodManager>()
?? throw new InvalidOperationException(
"Could not obtain InputMethodManager; the host view has no Context or the IME service is unavailable."); Defensive patterns
Strategy: validation
Validate before calling
// construct the input method only once the host view has a context if (host.Context is null) return; // defer until attached
Type guard
static bool HasImm(View v) => v.Context?.GetSystemService(Context.InputMethodService) is not null;
Try / catch
// construct lazily; resolve IMM on first Show so Context is available.
Prevention
- Build the input method after the view is attached (Context non-null).
- Inject a mock InputMethodManager in tests.
- Defer IMM resolution to first use.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Application.Context.MainLooper was not expected to be null.
- This class should be instanciated from the UI thread
- Avalonia Application was not initialized. Make sure you have
- Unknown error: AvaloniaView initialization has failed.
- Activity.Window must be set.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/4d86919da8b0aae0.
Report an issue: GitHub.