0

I am trying to make an EditText with a listener that checks the length of the text entered. If the EditText is not empty then the button should be enabled, otherwise it is empty must be disabled. To do this I wrote this code.

        final EditText editText = new EditText(context);
        builder.setView(editText);
        builder.setTitle("TItle");

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {



            }
        });

        builder.setNegativeButton("No", null);

        editText.addTextChangedListener(new TextWatcher() {

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

              public void onTextChanged(CharSequence s, int start, int before, int count) 
              {
                  AlertDialog dialog = builder.create();
                  dialog.show();
                  String text = editText.getText().toString();

                  if(text.trim().length()>0) {
                     button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                     button.setEnabled(false);
                  }
                  else {
                     button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                     button.setEnabled(false);
                  }
              }


            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
           });

Just write something in EditText crashes with a NullPointerException here: button.setEnabled(false);

Why? how can i solve?

Giovanni Mariotti
  • 615
  • 1
  • 7
  • 11

2 Answers2

2

Update

Seems like dialogo.getButton() is returning null in your case. I looked up, there is a similar issue related.

Basically you need dialogo.show() before you call getButton(). So update your code as:

builder.setNegativeButton("No", null);
final AlertDialog dialog = builder.create();
dialog.show();
editText.addTextChangedListener(new TextWatcher() {

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    public void onTextChanged(CharSequence s, int start, int before, int count) 
    {

        String text = editText.getText().toString();
        ...
        ...
        button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        if(button != null) {    
            if(text.trim().length()>0) {
                button.setEnabled(true);
            }
            else {
                button.setEnabled(false);
            }
        }
    }
    ...
...

Old Answer

Change your else to following:

...
...
else {
   button = dialogo.getButton(AlertDialog.BUTTON_POSITIVE);
   button.setEnabled(false);
}
...
...
Community
  • 1
  • 1
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
1

Initially when you click on the the edit text, the addTextChangedListener will be fired and since you have not entered and text text.trim().length()=0 so the control goes to the else block.

In your else block, you are not initializing the the button, hence the NPE.

You have write the following line in else block too

button = dialogo.getButton(AlertDialog.BUTTON_POSITIVE);
codeMan
  • 5,730
  • 3
  • 27
  • 51