問題描述
我有一個(gè)編輯文本,它在我的應(yīng)用程序中用作搜索框.在我的 Nexus 7 上的 Jelly Bean 中,當(dāng)我在我正在收聽的文本框中輸入一些內(nèi)容并點(diǎn)擊輸入 KeyEvent = null 和 ActionId = 0 傳遞給 onEditorAction() 方法.有人遇到過這種情況么?我認(rèn)為這可能是一個(gè)錯(cuò)誤.
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.
在下面的第二個(gè) if 語句中,我得到一個(gè)空指針,因?yàn)?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();
}
}
推薦答案
最終添加了對(duì) KeyEvent 的空檢查.感謝 commonsware 指出這發(fā)生在 3.0+ 上.看起來更像是一種解決方法而不是解決方案,但它確實(shí)有效.
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();
}
}
這篇關(guān)于onEditorAction() 中的 null keyevent 和 actionid = 0 (Jelly Bean/Nexus 7)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!