Search code examples
androidandroid-studiokeyevent

invoke virtual method 'int android.view.KeyEvent.getAction()' on a null object referenceid.view.KeyEvnt.getAction()


I have this code:

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

And is returning this error

at widget.ClearableEditText$1.onEditorAction(ClearableEditText.java:34)

How can I handle this?


Solution

  • Use a actionID instead of action_event. Hope it will be solved. Sample code below:-

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