如何修复导致addOnCompleteListener()启动的错误

时间:2019-02-10 17:47:36

标签: android firebase firebase-authentication

我已按照android studio中的说明将Firebase Auth添加到应用中。该代码正在添加新用户,但未显示其预期效果。我尝试调试代码,发现它没有输入“完成时”方法     在此处输入代码

public class Register extends AppCompatActivity implements View.OnClickListener,  OnCompleteListener<Void> {


private FirebaseAuth mAuth;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    mAuth = FirebaseAuth.getInstance();

}
@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
   // updateUI(currentUser);
}

@Override
public void onClick(View view) {
    CreateNewUser();
}
private void CreateNewUser()
{
    //creates a new user and adds it to firebase
    //uses the mail and password in the edit text
    final String TAG="tag";
    mAuth.createUserWithEmailAndPassword("example @gmail.com","1234567")
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Toast.makeText(Register.this,"Task is successful",Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();


                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(Register.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}

我希望有个祝酒词,因为“任务成功”已输入到Firebase用户数据库中

1 个答案:

答案 0 :(得分:0)

您是否为视图设置了onClickListener,该视图应触发CreateNewUser方法?您Activity正在实现该接口。在onCreate中,您应该调用类似的内容:

findViewById(R.id.myButton).setOnClickListener(this);

myButton是活动xml中定义的View的ID。