在 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
并将 View 动态设置为 v.requestFocus();
应用程序开始运行得稍微好一些,但有时仍会观察到警告,感觉系统对用户来说已挂起.查看下面的代码 fragment 了解详情:
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
LogUtils.d(TAG, String.valueOf(event.getKeyCode()));
this.getWindow().getDecorView().setFocusable(true);
return super.dispatchKeyEvent(event);
}
我的问题是,如何将丢失的焦点动态设置到 PopupMenu
和 RecyclerView
或者如何在 Android 中运行的应用程序中克服这个问题
请提出建议,我在 stackOverflow 中遇到了相同类型的问题,但没有合适的解决方案,因此发布问题并等待所有类型的建议和答案。 提前致谢。
最佳答案
在我的例子中,类似的问题是由在任何地方使用 onKeyDown() 和 KeyEvent.ACTION_DOWN 引起的,包括在按键时显示对话框时。 使用 onKeyUp() 和 KeyEvent.ACTION_UP 显示对话框和 Activity 为我解决了这个问题。
关于android - 在安卓 P : Dropping event due to no window focus: KeyEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702989/