UnsupportedOperationException: Tried to obtain display from a Context not associated with one
Asked Answered
G

3

15

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)
Globulin answered 30/5, 2021 at 6:3 Comment(7)
Can you isolate which Context is having getDisplay() called on it?Boyles
@Boyles 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.Globulin
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.Boyles
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?Prescott
@Globulin Did you get insight on this? we are getting the same crash but not reproducibleCaseous
@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.Rainbolt
@Globulin I have the same issue and I need to check if you are using custom AppCompatDelegate like that one #55266334Southward
T
4

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.

Turbary answered 6/4, 2023 at 0:34 Comment(1)
Thanks, you pointed out exactly the issue that i have encountered. This is not an easy to fix crash since the information is vague!Tephrite
M
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
    }

Marinna answered 12/7, 2022 at 17:48 Comment(2)
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 ReviewImplicate
i have the same issue and this is the full stacktrace :)Thickskinned
A
0

I am also encountering this issue, and the exception is due to the following source code.

public Display getDisplay() {
    if (!isAssociatedWithDisplay()) {
        throw new 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.");
    }
    return getDisplayNoVerify();
}

The exception is caused by overriding the attachBaseContext method and re-creating the content object. It is necessary to check if the generation method is reasonable.

Animator answered 28/11, 2023 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.