java - 不会将数据保存到 Android 中的 Firebase 数据库

标签 java android firebase firebase-realtime-database

我不确定问题出在哪里。我是一名初学者开发人员,我为我正在开发的 Android 应用程序编写了注册/登录页面。新用户将保存在 Firebase 授权 中,但不会保存在 Firebase 数据库 中。我当前的规则设置为 false,但当我尝试将它们设置为 true 时,应用程序不断返回到 SetupActivity 而不是 MainActivity。当规则设置为 false 时,应用程序运行良好,但正如我所说,数据库 中什么也没有出现。这是我的代码:

公共(public)类SetupActivity扩展了AppCompatActivity{

private EditText FullName, EmailAddress, Password, CountryName;
private Button SaveInfoButton;
private ProgressDialog LoadingBar;
private CircleImageView ProfileImage;
private FirebaseAuth register_auth;
private DatabaseReference userreference;
private String currentUserID;


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

    register_auth = FirebaseAuth.getInstance();
    currentUserID = register_auth.getCurrentUser().getUid();
    userreference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);

    FullName = findViewById(R.id.name_setup);
    EmailAddress = findViewById(R.id.email_setup);
    Password = findViewById(R.id.password_setup);
    CountryName = findViewById(R.id.country_setup);
    SaveInfoButton = findViewById(R.id.save_button);
    ProfileImage = findViewById(R.id.profile_setup);
    LoadingBar = new ProgressDialog(this);


    SaveInfoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)
        {
            CreateNewAccount();
        }
    });

}

private void CreateNewAccount() {
    String full_name = FullName.getText().toString();
    String email = EmailAddress.getText().toString();
    String password = Password.getText().toString();
    String country = CountryName.getText().toString();

    if(TextUtils.isEmpty(email)) {
        Toast.makeText(this, "Please enter email.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(full_name)) {
        Toast.makeText(this, "Please enter your name.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(password)) {
        Toast.makeText(this, "Please enter password.", Toast.LENGTH_SHORT).show();
    }
    else if(TextUtils.isEmpty(country)) {
        Toast.makeText(this, "Please enter country.", Toast.LENGTH_SHORT).show();
    }
    else {

        LoadingBar.setTitle("Creating new account!");
        LoadingBar.setMessage("Please wait while your account is being created.");
        LoadingBar.show();
        LoadingBar.setCanceledOnTouchOutside(true);

        register_auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()) {
                    LoadingBar.dismiss();
                    Toast.makeText(SetupActivity.this, "Registration was successful!", Toast.LENGTH_SHORT).show();
                    SaveAccountInformation();
                }
                else {
                    String message = task.getException().getMessage();
                    Toast.makeText(SetupActivity.this, "Registration unsuccessful." + message, Toast.LENGTH_SHORT).show();
                    LoadingBar.dismiss();
                }

            }
        });
    }
}

private void SaveAccountInformation() {

    String full_name = FullName.getText().toString();
    String country = CountryName.getText().toString();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("fullname", full_name);
    childUpdates.put("country", country);
    childUpdates.put("status", "Hey there, I am using Study Guide!");
    childUpdates.put("birthday", "none");

    userreference.updateChildren(childUpdates).addOnCompleteListener(new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            if (task.isSuccessful()) {
                SendToLogin();
            }
            else {
                String message = task.getException().getMessage();
                Toast.makeText(SetupActivity.this, "An error occurred. " + message, Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void SendToLogin() {
    Intent LoginIntent = new Intent(SetupActivity.this,LoginActivity.class);
    LoginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(LoginIntent);
    finish();
}

}

如果有人能指出我正确的方向或让我知道我做错了什么,我将非常感激!

最佳答案

Hazal 您没有保存数据,您正在更新数据,因此请更改您的代码

来自

userreference.updateChildren(childUpdates)

userreference.setValue(childUpdates)

关于java - 不会将数据保存到 Android 中的 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61587924/

相关文章:

firebase - 即使没有更改,Cloud Firestore : on realtime listeners, 也会收取读取费用?

javascript - Vuejs全局注册vuefire

java - 将 jvm 参数传递给 Gradle 测试任务

java - 错误 :(31, 39) 错误:找不到符号变量上下文

java - spring.jpa.properties.hibernate.jdbc.time_zone 适用于写入但不适用于读取?

android - Appflow 部署插件导致错误 : 'uncaught (in promise) string resource id #0x0' in Ionic Capacitor project

android - 有没有办法让 Activity 知道刚刚创建了什么 fragment ?

java - 在这种情况下如何向文件名添加特殊字符

android - 如何获取 dataclient.getDataItem(Uri) 自己的 node_id?

Android firebase 向用户段发送通知以保存数据