android – 在尝试将一些代码放入builder.setPositiveButton的onClick()方法时获取’无法解析方法’addOnCompletionListener()’…’

前端之家收集整理的这篇文章主要介绍了android – 在尝试将一些代码放入builder.setPositiveButton的onClick()方法时获取’无法解析方法’addOnCompletionListener()’…’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在AlertDialog.Builder的builder.setPositiveButton方法中放置一些代码.

问题是我收到以下错误:无法解析方法’addOnCompletionListener(匿名android.content.DialogInterface.OnClickListener,匿名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();

这有什么不对?

请告诉我.

解决方法

addOnCompleteListener(this,new OnCompleteListener<AuthResult>()

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

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

猜你在找的Android相关文章