有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java注册按钮给我的身份验证失败,我如何修复它?

“注册”按钮功能不正常,它给我身份验证失败的提示。似乎我为创建一个带有电子邮件和密码的用户而设置的字符串是正确的,但仍然不起作用

我能做什么?如果可以的话,请帮忙

Java文件:

package com.example.ahmadsapp;

import 安卓x.annotation.NonNull;
import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.app.DatePickerDialog;
import 安卓.app.Dialog;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.DatePicker;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
import 安卓.widget.Toast;

import com.google.安卓.gms.tasks.OnCompleteListener;
import com.google.安卓.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Calendar;

public class signup extends AppCompatActivity implements View.OnClickListener {
    EditText ETname, ETgmail, ETpassword;
    Button register,Bdateofbirth;
    SharedPreferences sp;
    private FirebaseAuth mAuth;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

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

        mAuth = FirebaseAuth.getInstance();
        firebaseDatabase = FirebaseDatabase.getInstance();

        sp = getSharedPreferences("detail", 0);
        String userdfn = sp.getString("firstname", null);
        String userdgamil = sp.getString("gmail", null);
        String userduser = sp.getString("user", null);
        String userdpass = sp.getString("password", null);

        ETname= findViewById(R.id.ETsignupname);
        ETgmail = findViewById(R.id.ETsignupgmail);
        ETpassword = findViewById(R.id.ETsignuppassword);
        register = findViewById(R.id.signupbutton);
        Bdateofbirth = findViewById(R.id.Bdateofbirth);




        register.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        if (view == register) {
            register(ETgmail.getText().toString(), ETpassword.getText().toString());
        }

        if (view == Bdateofbirth) {
            dialogDOB();
        }

    }





     private void register(String User, String Password) {
    
            mAuth.createUserWithEmailAndPassword(User, Password)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
    
                                addClassToFirebase();
    
                                Intent intent = new Intent(signup.this, MainActivity.class);
                                intent.putExtra("etsufn", ETname.getText().toString());
                                startActivity(intent);
    
    
                            } else {
                                // If sign in fails, display a message to the user.
                                Toast.makeText(signup.this, "Authintication failed", Toast.LENGTH_SHORT).show();
                            }
    
                            // ...
                        }
                    });
    
        }
    
        private void addClassToFirebase() {
            String uid = mAuth.getCurrentUser().getUid().toString();
            User u = new User(uid, ETname.getText().toString(), ETgmail.getText().toString(), ETpassword.getText().toString(), Bdateofbirth.getText().toString(), "");
            databaseReference = firebaseDatabase.getReference("Users").push();
            u.key = databaseReference.getKey();
            databaseReference.setValue(u);
        }
        private void dialogDOB(){
    
            Calendar calendar = Calendar.getInstance();
            int d = calendar.get(Calendar.DAY_OF_MONTH);
            int m = calendar.get(Calendar.MONTH);
            int y = calendar.get(Calendar.YEAR);
            DatePickerDialog datePickerDialog = new DatePickerDialog(signup.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                    i1=i1+1;
                    String date = i + "/" + i1 + "/" + i2;
                    Toast.makeText(signup.this, date, Toast.LENGTH_SHORT).show();
                    Bdateofbirth.setText(date);
                }
            }, d, m, y);
            datePickerDialog.show();
        }
    
    
    }

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".signup"
    安卓:orientation="vertical">

    <TextView
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:text="Welcome to the signup page"
        安卓:layout_gravity="center"
        安卓:textSize="25dp"
        安卓:id="@+id/TVsignup"/>

    <EditText
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:hint="name"
        安卓:textSize="25dp"
        安卓:gravity="center"
        安卓:layout_gravity="center"
        安卓:id="@+id/ETsignupname"
        />

    <EditText
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:hint="G-Mail"
        安卓:textSize="25dp"
        安卓:gravity="center"
        安卓:layout_gravity="center"
        安卓:id="@+id/ETsignupgmail"
        />

    <EditText
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:hint="Password"
        安卓:textSize="25dp"
        安卓:gravity="center"
        安卓:layout_gravity="center"
        安卓:id="@+id/ETsignuppassword"
        />

    <Button
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:id="@+id/Bdateofbirth"
        安卓:text="Date of birth"
        安卓:gravity="center"
        安卓:onClick="onClick"
        安卓:layout_gravity="center"/>

    <Button
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:hint="Register"
        安卓:layout_gravity="center"
        安卓:id="@+id/signupbutton"
        />

</LinearLayout>

我填写的字符串:

[在此处输入图像描述][1] [1]: https://i.stack.imgur.com/2d29O.png


共 (1) 个答案

  1. # 1 楼答案

    当任务失败时,它包含一个解释失败原因的异常。记录此原因以了解问题原因的更多信息:

    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
    
            addClassToFirebase();
    
            Intent intent = new Intent(signup.this, MainActivity.class);
            intent.putExtra("etsufn", ETname.getText().toString());
            startActivity(intent);
    
    
        } else {
            // If sign in fails, display a message to the user.
            Toast.makeText(signup.this, "Authintication failed", Toast.LENGTH_SHORT).show();
            Log.e("AUTH", "Authentication failed", task.getException()); // 👈
        }
    

    另请参阅^{} class的参考文档