android-将自定义ID添加到Firestore文档

时间:2018-09-23 15:48:05

标签: java android firebase google-cloud-firestore

我正在研究一个包含如下代码的项目:How to add Document with Custom ID to firestore (Angular)

当我要尝试该应用程序时,它总是崩溃。但是代码都是一样的。

我在LogCat中遇到的错误是:

  

无效的文档参考。文档引用必须具有偶数个段,但用户具有1

我的完整代码是这样:

public class RegisterActivity extends AppCompatActivity {


private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private EditText registerConfirmPassword;
private Button registerRegisterButton;
private Button registerLoginButton;
private ProgressBar registerProgressBar;

private FirebaseFirestore firebaseFirestore;

private String user_id;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    registerUsername = findViewById(R.id.register_username);
    registerEmail = findViewById(R.id.register_email);
    registerPassword = findViewById(R.id.register_password);
    registerConfirmPassword = findViewById(R.id.register_confirm_password);
    registerRegisterButton = findViewById(R.id.register_register_button);
    registerLoginButton = findViewById(R.id.register_login_button);
    registerProgressBar = findViewById(R.id.register_progressBar);

    firebaseFirestore = FirebaseFirestore.getInstance();

    user_id = registerUsername.getText().toString();

    registerLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(loginIntent);

        }
    });

    registerRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String username = registerUsername.getText().toString();
            String email = registerEmail.getText().toString();
            String password = registerPassword.getText().toString();
            String confirmPassword = registerConfirmPassword.getText().toString();

            if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword)) {

                if (password.equals(confirmPassword)) {

                    registerProgressBar.setVisibility(View.VISIBLE);

                    Map<String, String> usersMap = new HashMap<>();
                    usersMap.put("username", username);
                    usersMap.put("email", email);
                    usersMap.put("password", password);

                    firebaseFirestore.collection("Users").document(user_id).set(usersMap).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {

                            Toasty.success(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_SHORT).show();

                            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(loginIntent);

                            registerProgressBar.setVisibility(View.INVISIBLE);

                        }
                    });

                } else {

                    Toasty.error(RegisterActivity.this, "Passwords Don't Match", Toast.LENGTH_SHORT).show();

                }

            }

        }
    });
}}

我希望“ user_id”是文档ID,而不是Firestore生成的另一个ID。

有人可以帮忙吗?并预先感谢。

1 个答案:

答案 0 :(得分:0)

您可能在onCreate的开头设置了user_id,可能是在EditText registerUsername不包含任何文本的时候。这意味着user_id始终为空,无论此活动发生什么。

您不能将空字符串传递给document()。您需要一个实际的字符串值,该值也是valid Firestore document ID。填写表单后,请尝试在registerUsername中获取字符串值,而不是之前。