在尝试将一些代码放在builder.setPositiveButton的onClick()方法中时,获取'无法解析方法'addOnCompletionListener()'.......'

Ham*_*sir 10 android android-alertdialog firebase firebase-authentication

我试图把里面的一些代码AlertDialog.Builderbuilder.setPositiveButton方法.

问题是我收到以下错误: Cannot resolve method 'addOnCompletionListener(anonymous android.content.DialogInterface.OnClickListener, anonymous com.google.android.gms.tasks.OnCompletionListener<com.google.firebase.auth.AuthResult>)

这是代码:

    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();
Run Code Online (Sandbox Code Playgroud)

这有什么不对?

请告诉我.

Ins*_*nso 24

addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
Run Code Online (Sandbox Code Playgroud)

这行中的"this"表示你的DialogInterface.OnClickListener,你应该检查这个方法需要什么类型的params,如果是Context,试着把它改成这个

addOnCompleteListener(YourActivityName.this, new OnCompleteListener<AuthResult>()
Run Code Online (Sandbox Code Playgroud)


Has*_*ter 5

此行代码发生了您的问题:

.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
Run Code Online (Sandbox Code Playgroud)

在这里,this参数编译器让.OnClickListener您了解了活动上下文。

在此处输入图片说明

solutin非常简单:

.addOnCompleteListener(ActivityName.this, new OnCompleteListener<AuthResult>() {
Run Code Online (Sandbox Code Playgroud)

安装this使用ActivityName.this