简体   繁体   中英

Android onEditorAction force close

I'm trying to perform some functions once the user presses "Next" on their keyboard but I'm having some issues. The app compiles, but crashes once "Next" is pressed.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText etGhs = (EditText) findViewById(R.id.etGhs);

    // Calculate DGM once the user has finished typing
    etGhs.setOnEditorActionListener(
            new EditText.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event.getAction() == KeyEvent.ACTION_DOWN &&
                                    event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                        if (!event.isShiftPressed()) {
                            // the user is done typing.
                            // get string from etGhs
                            double ghs = Integer.parseInt(etGhs.getText().toString());
                            double dgm = ghs * 0.000064179; // calculate avg DGM

                            setDgmAndDailyBtc(dgm);
                            return true; // consume.
                        }
                    }
                    return false; // pass on to other listeners.
                }
            });
}

Here is the logcat

03-11 20:51:09.847  11528-11528/com.devinshoe.bitcoinprofit.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.devinshoe.bitcoinprofit.app, PID: 11528
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getAction()' on a null object reference
        at com.devinshoe.bitcoinprofit.app.MainActivity$1.onEditorAction(MainActivity.java:27)
        at android.widget.TextView.onEditorAction(TextView.java:4236)
        at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
        at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:297)
        at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

Thanks in advance for any help.

An old question, but if you are still looking for an answer, the following has worked for me. The part where you check for "Enter" options, ie

if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            event.getAction().....

You need to add more search options, ie

if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                            actionId == EditorInfo.IME_ACTION_DONE ||
                            actionId == EditorInfo.IME_ACTION_GO ||
                            actionId == EditorInfo.IME_ACTION_NEXT ||
                            event.getAction().....

My keyboard was setup to take the default action as IME_ACTION_GO, but just to be on a safer side, I left the other options in there.

event is null. You need to check for that and not call getAction on it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM