首页 > 解决方案 > 在 Android P 中:由于没有窗口焦点而丢弃事件:KeyEvent

问题描述

Android P,应用程序开始无法按预期工作并抛出警告:

 W/ViewRootImpl: 
    Dropping event due to no window focus: MotionEvent { action=ACTION_DOWN, KeyCode = DPAD_DOWN} 

W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_UP, KeyCode = DPAD_DOWN} and its not allowing any action to performed.

检查图像以获取详细警告。

注意:相同的应用程序在 中运行良好Android O,但在 中Android P,有时项目中缺少焦点, PopupMenu并且在重新启动系统之前无法恢复焦点。使用 dispatchKeyEvent并动态设置视图焦点后, v.requestFocus(); 应用程序开始工作得稍微好一点,但有时仍会观察到警告,感觉就像系统为用户挂起一样。查看下面的代码片段了解详情:

@Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            LogUtils.d(TAG, String.valueOf(event.getKeyCode()));
            this.getWindow().getDecorView().setFocusable(true);
            return super.dispatchKeyEvent(event);
        }

我的问题是,如何在 Android P 中运行的应用程序中动态设置缺少的焦点 PopupMenuRecyclerView 或者如何克服这个问题。

请建议,我在 stackOverflow 中遇到了相同类型的问题,但不是正确的解决方案,因此发布问题并等待所有类型的建议和答案。提前致谢。在此处输入图像描述

标签: androidandroid-studiopopupmenu

解决方案


在我的情况下,类似的问题是由于在任何地方使用 onKeyDown() 和 KeyEvent.ACTION_DOWN 引起的,包括在按键上显示对话框时。使用 onKeyUp() 和 KeyEvent.ACTION_UP 显示对话框和活动为我解决了这个问题。


推荐阅读