Hot questions for Using Butter Knife in android edittext

Top 10 Java Open Source / Butter Knife / android edittext

Question:

I am trying to use the interface OnEditorAction from ButterKnife for the "ENTER" event of EditText

@OnEditorAction(R.id.et_good)
boolean onEditorAction(EditText editText, int actionId, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.KEYCODE_ENTER) {
        updateArticle(editText.getText().toString().toUpperCase().trim());
    }
    return keyEvent.getAction() == KeyEvent.KEYCODE_ENTER;
}

However I keep getting EXCEPTION where

com.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxx.xxx, PID: 6391
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getAction()' on a null object reference
    at com.xxx.xxx.MainActivity.onEditorAction(MainActivity.java:68)
    at com.xxx.xxx.MainActivity_ViewBinding$1.onEditorAction(MainActivity_ViewBinding.java:47)
    at android.widget.TextView.onEditorAction(TextView.java:5911)
    at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Layout xml file for your reference as well.

<EditText
    android:id="@+id/et_good"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="8dp"
    android:paddingStart="10dp"
    android:singleLine="true"
    android:imeOptions="actionDone"
    app:layout_constraintEnd_toStartOf="@+id/xxx"
    app:layout_constraintStart_toEndOf="@+id/xxx"
    app:layout_constraintTop_toBottomOf="@+id/xxx" />

Kindly advice what do I miss out.


Answer:

Try this, Since you have android:imeOptions="actionDone" defined in your xml this should work.

@OnEditorAction(R.id.et_good)
boolean onEditorAction(EditText editText, int actionId, KeyEvent keyEvent) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        updateArticle(editText.getText().toString().toUpperCase().trim());
    }
    return actionId == EditorInfo.IME_ACTION_DONE;
}

Question:

I have a dialog that pops up at first run and prompts the user to enter his/her username inside an editText view but whenever i try to access it's text by doing editText.getText(); i get a NullPointerException saying that the editText is null.What am i missing here? I tried inflating the layout and accessing the .getText() method from an Activity but nothing. Here's my code:

public class UsernameDialog extends DialogFragment {

@BindView(R.id.editText) EditText mEditText;
@BindView(R.id.username)
TextView menu_usr;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    // inflate the layout using the dialog themed context
    final Context context = getActivity();
    final LayoutInflater inflater = LayoutInflater.from(context);
    final View view = inflater.inflate(R.layout.username_dialog,null,false);

    DialogInterface.OnClickListener posListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            menu_usr.setText(mEditText.getText());
            Log.d("USERNAME",mEditText.getText().toString());
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
            .setTitle("Choose your username")
            .setView(view)
            .setPositiveButton("OK",posListener);
    return builder.create();
}
}

Error log:

  Process: com.dcv.spdesigns.dokkancards, PID: 373
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
    at com.dcv.spdesigns.dokkancards.ui.UsernameDialog$1.onClick(UsernameDialog.java:48)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:177)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Answer:

Add

Butterknife.bind(this,view)