空的KeyEvent和actionid = 0 onEditorAction()(果冻豆/的Nexus 7)果冻、actionid、KeyEvent、Nexus

2023-09-04 04:54:47 作者:女人 别太做作ら

我有它的功能在我的应用程序搜索框编辑文本。在果冻豆在我的Nexus 7,当我输入一些东西到我正在听的文​​本框,并按下回车键该KeyEvent = null并且ActionId = 0传递到onEditorAction()方法。有其他人遇到过吗?我想这可能是一个错误。

在第二个if语句下面我得到一个空指针,因为actionId = 0和KeyEvent的= NULL;

  //搜索领域的逻辑。
@覆盖
公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
    Log.d(TAG,onEditorAction);
    如果(事件= NULL和放大器;!&安培;!event.getAction()= KeyEvent.ACTION_DOWN)
        返回false;
    如果(actionId == EditorInfo.IME_ACTION_SEARCH
            || event.getKey code()== KeyEvent.KEY code_ENTER){
              .....做一些东西();
     }
}
 

解决方案

结束了在一个空检查KeyEvent的增加。感谢commonsware指出这个发生在3.0+。似乎更像是一种解决方法,然后一个解决方案,但它的工作原理。

  //搜索领域的逻辑。
@覆盖
公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
    Log.d(TAG,onEditorAction);
    如果(事件= NULL和放大器;!&安培;!event.getAction()= KeyEvent.ACTION_DOWN){
        返回false;
    }否则,如果(actionId == EditorInfo.IME_ACTION_SEARCH
        ||事件== NULL
        || event.getKey code()== KeyEvent.KEY code_ENTER){
              .....做一些东西();
    }
}
 
2020 6 1 策略解析

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.

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();
     }
}

解决方案

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();
    }
}