null keyevent and actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)(onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly Bean/Nexus 7))
问题描述
我有一个编辑文本,它在我的应用程序中用作搜索框.在我的 Nexus 7 上的 Jelly Bean 中,当我在我正在收听的文本框中输入一些内容并点击输入 KeyEvent = null 和 ActionId = 0 传递给 onEditorAction() 方法.有人遇到过这种情况么?我认为这可能是一个错误.
I have an edit text which functions as a search box in my application. In Jelly Bean on my Nexus 7 when I type something into the text box which I am listening on and hit enter the KeyEvent = null and ActionId = 0 passed into the onEditorAction() method. Has anyone else encountered this? I'm thinking it might be a bug.
在下面的第二个 if 语句中,我得到一个空指针,因为 actionId = 0 和 KeyEvent = null;
In the second if statement below I get a null pointer because the actionId = 0 and KeyEvent = null;
// Search field logic.
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "onEditorAction");
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN)
return false;
if (actionId == EditorInfo.IME_ACTION_SEARCH
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
.....Do some stuff();
}
}
推荐答案
最终添加了对 KeyEvent 的空检查.感谢 commonsware 指出这发生在 3.0+ 上.看起来更像是一种解决方法而不是解决方案,但它确实有效.
Ended up adding in a null check for KeyEvent. Thanks to commonsware for pointing out this happens on 3.0+. Seems more like a workaround then a solution, but it works.
// Search field logic.
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "onEditorAction");
if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
return false;
} else if (actionId == EditorInfo.IME_ACTION_SEARCH
|| event == null
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
.....Do some stuff();
}
}
这篇关于onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly Bean/Nexus 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly


- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01