android - 在 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 中运行的应用程序中动态设置缺少的焦点 PopupMenu
,RecyclerView
或者如何克服这个问题。
请建议,我在 stackOverflow 中遇到了相同类型的问题,但不是正确的解决方案,因此发布问题并等待所有类型的建议和答案。提前致谢。
解决方案
在我的情况下,类似的问题是由于在任何地方使用 onKeyDown() 和 KeyEvent.ACTION_DOWN 引起的,包括在按键上显示对话框时。使用 onKeyUp() 和 KeyEvent.ACTION_UP 显示对话框和活动为我解决了这个问题。
推荐阅读
- monaco-editor - 在悬停消息中不显示 marker.message 和 marker.code
- javascript - 如何根据子元素的动态值导航到页面的特定部分?
- python - Python CLI 字符串格式化成列
- javascript - 将对象数组中的小时转换为分钟
- javascript - 在“npm 链接”和“npm i”之后
" 无法解析模块("main" 和 "types" 在 package.json 中正确) - curl - 您可以使用例如 nc 或 wget 而不使用 curl 来发出 http 请求吗?(忙箱)
- spring - Spring Security 如何决定 WebExpressionVoter 投票的“表达式”?
- apache-spark - PySpark :- 如何将编码格式传递给 sc.newAPIHadoopFile 配置
- python - Python-Request - 尝试请求网站数据时收到指针错误
- npm - 带有 npm 的命令行参数可以消除对 `.npmrc` 文件的需要吗?