androidandroid-edittextenterforceclose

Force closed while implementing enter as an action


I tried to use enter in EditText as an action, but i always get force closed. Please tell me know where is the wrong code from my program.

Here is my code.

noteField = (EditText)findViewById(R.id.noteNote);
noteField.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){
            builder.setTitle("Saved")
                    .setMessage("Confirm saved notes?")
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            onSaved();
                        }
                    })
                    .setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setNeutralButton("Not yet", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    }).setCancelable(true).show();
            return true;
        }
        return false;
    }
});

Logcat.

FATAL EXCEPTION: main
  Process: com.fstudio.kamussakumatematika, PID: 28936
  java.lang.NullPointerException
      at com.fstudio.kamussakumatematika.NewNote$1.onKey(NewNote.java:35)
      at android.view.View.dispatchKeyEvent(View.java:7706)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1446)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2076)
      at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1531)
      at android.app.Activity.dispatchKeyEvent(Activity.java:2451)
      at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:547)
      at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
      at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319)
      at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2003)
      at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4436)
      at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4404)
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3958)
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4008)
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3977)
      at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4084)
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3985)
      at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4141)
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3958)
      at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4008)
      at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3977)
      at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3985)
      at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3958)
      at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6290)
      at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6253)
      at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6207)
      at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3744)
      at android.os.Handler.dispatchMessage(Handler.java:110)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:5299)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
      at dalvik.system.NativeStart.main(Native Method)

Solution

  • Initialize the AlertDialog.Builder as follows before you use it.

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_UP) && (keyCode == KeyEvent.KEYCODE_ENTER)){
            AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
            ...
        }
    }