在空对象 referenceid.view.KeyEvnt.getAction() 上调用虚方法 'int android.view.KeyEvent.getAction()'
invoke virtual method 'int android.view.KeyEvent.getAction()' on a null object referenceid.view.KeyEvnt.getAction()
我有这个代码:
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setOnEditorActionListener((v, actionId, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return true;
}
return false;
});
并返回此错误
at
widget.ClearableEditText.onEditorAction(ClearableEditText.java:34)
我该如何处理?
使用 actionID
而不是 action_event
。希望它会得到解决。示例代码如下:-
your_edittextfield.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView edt, int actionId, KeyEvent key) {
boolean handled=false;
if(actionId==EditorInfo.IME_ACTION_DONE) {
//Do your task here
handled=true;
}
return handled;
}
});
我有这个代码:
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setOnEditorActionListener((v, actionId, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
return true;
}
return false;
});
并返回此错误
at widget.ClearableEditText.onEditorAction(ClearableEditText.java:34)
我该如何处理?
使用 actionID
而不是 action_event
。希望它会得到解决。示例代码如下:-
your_edittextfield.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView edt, int actionId, KeyEvent key) {
boolean handled=false;
if(actionId==EditorInfo.IME_ACTION_DONE) {
//Do your task here
handled=true;
}
return handled;
}
});