Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Adding Users Using Parse.com Error Messages with Dialogs

Added one more field 'confirm_password" its showing dialogue for every empty box except confirm_password? is it a bug?

while creating dialog box in signup activity, i added one more text field called confirmpassword and initialized the same in signup activity and did the same process as for all the other three text fields and also included in if condition but it showing error dialog for every empty field except for confirmpassword field. Developing in Android SDK

2 Answers

Pablo Rocha
Pablo Rocha
10,142 Points

in your onclicklistener you have the following:

            String confirmPassword = mConfirmPassword.toString();

Try changing it to this:

            String confirmPassword = mConfirmPassword.getText().toString();
Pablo Rocha
Pablo Rocha
10,142 Points

Also, your comparison with password and confirmPassword might be causing the issue depending on what fields you are filling out. If they do not equal, then you are only executing the code right below that condition and all the elseif's are skipped. It would be best to add a separate if statement to handle that comparison.

Harry James
Harry James
14,780 Points

Thanks Jose!

I'm marking your answer as the Best Answer as it is the solution to this question :)

public class SignUp_Activity extends ActionBarActivity {

protected EditText mUsername;
protected EditText mEmail;
protected EditText mPassword;
protected EditText mConfirmPassword;
protected Button mSignUpButton;


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

    mUsername = (EditText)findViewById(R.id.signUp_username);
    mEmail = (EditText) findViewById(R.id.signUp_email);
    mPassword = (EditText) findViewById(R.id.signUp_password);
    mConfirmPassword = (EditText) findViewById(R.id.signUp_confirmPassword);
    mSignUpButton = (Button) findViewById(R.id.signUp_button);

    mSignUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String username = mUsername.getText().toString();
            String password = mPassword.getText().toString();
            String confirmPassword = mConfirmPassword.toString();
            String email = mEmail.getText().toString();

            username = username.trim();
            password = password.trim();
            confirmPassword = confirmPassword.trim();
            email = email.trim();

            if(!password.equals(confirmPassword)) {

                //Set Message that password is not equal
            }
            else if(username.isEmpty() || email.isEmpty() || confirmPassword.isEmpty() || password.isEmpty()){

                AlertDialog.Builder builder = new AlertDialog.Builder(SignUp_Activity.this);
                builder.setMessage(getString(R.string.signUp_error_message));
                builder.setTitle(getString(R.string.SignUp_error_tittle));
                builder.setPositiveButton(android.R.string.ok, null);

                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else{
                //Create new user
                ParseUser newUser = new ParseUser();
                newUser.setUsername(username);
                newUser.setEmail(email);
                newUser.setPassword(password);
                newUser.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {

                        if(e==null){
                            //success!
                            Intent intent = new Intent(SignUp_Activity.this, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                        }
                        else{
                            AlertDialog.Builder builder = new AlertDialog.Builder(SignUp_Activity.this);
                            builder.setMessage(e.getMessage());
                            builder.setTitle(getString(R.string.SignUp_error_tittle));
                            builder.setPositiveButton(android.R.string.ok, null);

                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }

                    }
                });


            }
        }
    });
}

I got my mistake. Thanks