14

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)
Shahal
  • 1,008
  • 1
  • 11
  • 29
  • 1
    Can you isolate which `Context` is having `getDisplay()` called on it? – msbit May 30 '21 at 06:13
  • @msbit getDisplay() in the log, is an system call not from app. From what I know, onKeyUp is the one triggering the issue. onKeyUp is keyboard navigation buttons. – Shahal May 30 '21 at 06:30
  • Yes, makes sense from the logs, but if you can't isolate which `Context` or `Activity` this is being called on, you won't be able to get very far. – msbit May 30 '21 at 06:39
  • After a bit googling I found a thread of a different app who also encountered this on Android 11, maybe you can create an emulator with Android 11 and see if you can reproduce the error there? – Dan Baruch Jun 30 '21 at 14:17
  • @Shahal Did you get insight on this? we are getting the same crash but not reproducible – Navin Samuel Jul 15 '21 at 08:45
  • @NavinSamuel can you confirm the current status of your issue and whether you managed to get it resolved somehow? We're facing the same issue. – Abdul Mateen Sep 14 '21 at 11:46
  • 1
    @Shahal I have the same issue and I need to check if you are using custom AppCompatDelegate like that one https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx – Jemo Mgebrishvili Dec 17 '21 at 14:08

2 Answers2

1

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.

M.Ed
  • 969
  • 10
  • 12
0

As a workaround you can override onKeyUp or onKeyDown (unfortunately I can't see your full stackTrace so I do not know which method you need to override) in the Activity and return true/false in it like this

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }
    
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return false
    }

OneXeor
  • 31
  • 1
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32231594) – markspace Jul 18 '22 at 16:20
  • i have the same issue and this is the full stacktrace :) – ghita Sep 29 '22 at 09:58