Firebase不会在数据库中创建新的用户数据

时间:2019-06-27 12:28:45

标签: android firebase firebase-realtime-database firebase-authentication

我写了一个代码,让人们注册我,但是当我按下按钮时,数据库中没有创建新用户(它保持为空)。

这是SignUpActivity.java

public class SignUpActivity extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private EditText userEmail, userPassword1, userPassword2, userCode, userName, userDescription;
    private ImageView image;
    private ProgressDialog pDialog;
    private boolean imagePressed = false;
    private Uri filePath;
    private FirebaseStorage storage;
    private StorageReference storageReference;

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

        userCode = findViewById(R.id.userCode);
        userDescription = findViewById(R.id.userDescription);
        userEmail = findViewById(R.id.userEmail);
        userName = findViewById(R.id.userName);
        userPassword1 = findViewById(R.id.userPassword1);
        userPassword2 = findViewById(R.id.userPassword2);
        image = findViewById(R.id.userImageAdd);

        mAuth = FirebaseAuth.getInstance();

        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imagePressed = true;
                chooseImage();
            }
        });
    }

    private void chooseImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), Finals.PICK_IMAGE_REQUEST);

    }

    public void back(View view) {
        finish();
    }

    public void submit(View view) {
        pDialog = new ProgressDialog(this);
        if (TextUtils.isEmpty(userEmail.getText().toString())) {
            Toast.makeText(SignUpActivity.this, "Please enter email.", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(userPassword1.getText().toString())) {
            Toast.makeText(SignUpActivity.this, "Please enter password.", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(userPassword2.getText().toString())) {
            Toast.makeText(SignUpActivity.this, "Please repeat password.", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(userName.getText().toString())) {
            Toast.makeText(SignUpActivity.this, "Please enter user name", Toast.LENGTH_SHORT).show();
        } else if (!userPassword1.getText().toString().equals(userPassword2.getText().toString())) {
            pDialog.dismiss();
            Toast.makeText(SignUpActivity.this, "The passwords don't match.", Toast.LENGTH_SHORT).show();
        } else {
            pDialog.setMessage("Creating account...");
            pDialog.show();
            pDialog.setCanceledOnTouchOutside(false);

            if (imagePressed && filePath != null) {
                final StorageReference ref = storageReference.child("users/" + UUID.randomUUID().toString());
                ref.putFile(filePath).continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                    @Override
                    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                            throw task.getException();
                        }
                        return ref.getDownloadUrl();
                    }
                }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                    @Override
                    public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                            Uri downUri = task.getResult();
                            boolean isTrainer = trainer();
                            register(userEmail.getText().toString(), userPassword1.getText().toString(), userName.getText().toString(), userDescription.getText().toString(),
                                    downUri.toString(), isTrainer);
                        }
                    }
                });
            } else {
                boolean isTrainer = trainer();
                register(userEmail.getText().toString(), userPassword1.getText().toString(), userName.getText().toString(), userDescription.getText().toString(),
                        Finals.DEFAULT_USER_IMAGE_URL, isTrainer);
                finish();
            }
        }
    }

    private boolean trainer() {
        boolean trainer = false;
        if (!TextUtils.isEmpty(userCode.getText())) {
            if (userCode.getText().toString().equals(Finals.T_CODE))
                trainer = true;
            else if (!userCode.getText().toString().equals(Finals.T_CODE)) {
                pDialog.dismiss();
                Toast.makeText(this, "Not a real trainer code!", Toast.LENGTH_SHORT).show();
            }
        }
        return trainer;
    }

    private void register(final String regEmail, final String regPassword,
                          final String regName, final String regDesc, final String url, final boolean trainer) {
        mAuth.createUserWithEmailAndPassword(regEmail, regPassword).addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    FirebaseUser firebaseUser = mAuth.getCurrentUser();
                    String userId = firebaseUser.getUid();
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("id", mAuth.getCurrentUser().getUid());
                    hashMap.put("name", regName);
                    hashMap.put("description", regDesc);
                    hashMap.put("isTrainer", trainer);
                    hashMap.put("imageUrl", url);
                    FirebaseDatabase.getInstance().getReference().child("users").child(userId).setValue(hashMap)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {
                            if (task.isSuccessful()) {
                                pDialog.dismiss();
                                Toast.makeText(SignUpActivity.this, "Created user", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(SignUpActivity.this, MainActivity.class));
                            } else {
                                pDialog.dismiss();
                                Toast.makeText(SignUpActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                } else {
                    pDialog.dismiss();
                    Toast.makeText(SignUpActivity.this, "Creation failed :" + task.getException(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Finals.PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null) {
            filePath = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                image.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这曾经可以在我以前的手机上使用,但现在在新手机上无法使用。 什么可能导致此错误?

进度对话框也不会显示。

当我尝试访问代码时(在此行中为DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users").child(id);),它返回null

我也没有收到“出了点问题”,它只是转到了调用它的LoginActivity(无论出于什么原因都没有main Activity),然后我可以使用新的登录我创建的用户,但无法访问数据(它是null)。

这些是数据库规则: { "rules": { ".read": true, ".write": true } }

1 个答案:

答案 0 :(得分:0)

以某种方式,重写代码以不同的方式修复了该错误

新代码-

overflow: hidden;

非常感谢所有尝试提供帮助的人!