Getting 'Cannot resolve method 'addOnCompletionListener()'.......' while trying to place some code inside builder.setPositiveButton's onClick() method

16,081

Solution 1

addOnCompleteListener(this, new OnCompleteListener<AuthResult>()

"this" in this line means your DialogInterface.OnClickListener , you should check what kind of params this method needs, if Context, try to change it to this

addOnCompleteListener(YourActivityName.this, new OnCompleteListener<AuthResult>()

Solution 2

Your issue happen with this line code:

.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {

Here this parameter compiler let as .OnClickListener insted of your activity context.

enter image description here

The solutin is so simple:

.addOnCompleteListener(ActivityName.this, new OnCompleteListener<AuthResult>() {

Insted of this use ActivityName.this.

Share:
16,081
Hammad Nasir
Author by

Hammad Nasir

Updated on June 07, 2022

Comments

  • Hammad Nasir
    Hammad Nasir about 2 years

    I'm trying to place some code inside AlertDialog.Builder's builder.setPositiveButton method.

    The problem is that I'm getting the following error: Cannot resolve method 'addOnCompletionListener(anonymous android.content.DialogInterface.OnClickListener, anonymous com.google.android.gms.tasks.OnCompletionListener<com.google.firebase.auth.AuthResult>)

    Here's the code:

        AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
                    builder.setTitle("Title");
                    builder.setView(R.layout.customlayout);
                    builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
    
    //error from below line
    
                       mAuth.createUserWithEmailAndPassword(userEmail.getText().toString(), userPassword.getText().toString())
                                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                                        @Override
                                        public void onComplete(@NonNull Task<AuthResult> task) {
                                            Log.d("signUpSuccessful", "createUserWithEmail:onComplete:" + task.isSuccessful());
    
                                            // If sign in fails, display a message to the user. If sign in succeeds
                                            // the auth state listener will be notified and logic to handle the
                                            // signed in user can be handled in the listener.
                                            if (!task.isSuccessful()) {
                                                Snackbar snackbar = Snackbar
                                                        .make(coordinatorLayout, "Sign up failed. Please retry.", Snackbar.LENGTH_SHORT);
                                                snackbar.show();
                                            }
    
                                            // ...
                                        }
                                    });
    
    //upto this line
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
    

    What's wrong here?

    Please let me know.

  • Karlo A. López
    Karlo A. López over 6 years
    Excellent, I was copying exactly what Google documentation has. ;)