I'm getting UnsupportedOperationException crash on live app. All the crashes are associated with Moto Android 11 devices. Can see that it's somehow related to onKeyUp. But still no clue how to reproduce or fix this. Any help would be appreciated.
Fatal Exception: java.lang.UnsupportedOperationException: Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.
at android.app.ContextImpl.getDisplay(ContextImpl.java:2580)
at android.content.ContextWrapper.getDisplay(ContextWrapper.java:1030)
at android.content.ContextWrapper.getDisplay(ContextWrapper.java:1030)
at android.app.Activity.onKeyUp(Activity.java:3859)
at android.view.KeyEvent.dispatch(KeyEvent.java:2866)
at android.app.Activity.dispatchKeyEvent(Activity.java:4176)
at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:418)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:6101)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5969)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5464)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5521)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5487)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5639)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5495)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5696)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5468)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5521)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5487)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5495)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5468)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8313)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8229)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8190)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:5219)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:250)
at android.app.ActivityThread.main(ActivityThread.java:7766)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Make sure you're not setting a base context that's not associated with a display when you override attachBaseContext inside your activity, non-visual contexts like application or service contexts are no-gos, this will cause a runtime crash on a good chunk of Motorola devices when you hit certain keys on the keypad/keyboard. G30, G60, G60s in my case, note that some Motorola devices are unaffected.
So in your activity when you're overriding attachBaseContext() make sure that you're actually returning the right context inside your someMethodThatReturnsNewBaseContext() method.
override fun attachBaseContext(newBase: Context) {
val newBaseContext = someMethodThatReturnsNewBaseContext(newBase)
super.attachBaseContext(newBaseContext)
}
I found out that I was returning context.applicationContext.createConfigurationContext() inside my someMethodThatReturnsNewBaseContext() implementation, once I directly returned context.createConfigurationContext() the issue was resolved on Motorola devices.
Only (mostly?) contexts from activities are associated with a display because Activity actually inherits from ContextThemeWrapper, while other context components such as Application and Service only inherit ContextWrapper, hence they're considered "non-visual".
If you're not doing anything fancy when overriding attachBaseContext(), then look through your codebase anywhere where you're passing or calling non-activity context's methods, and you'll find the culprit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With