【发布时间】:2018-09-18 15:09:09
【问题描述】:
运行此代码时出现以下错误:
Invalid document reference. Document references must have an even number of segments
但 mylist 有 1"。
谁能帮帮我。
public class LoginActivity extends AppCompatActivity {
TextView textNewUser, textForgotPassword;
TextInputEditText username, password;
Button loginBtn;
String passwordTxtBx, usernameTxtBx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.login);
username = findViewById(R.id.userNameInputTxt);
password = findViewById(R.id.passwordInputTxt);
loginBtn = findViewById(R.id.loginBtn);
usernameTxtBx = username.getText().toString();
passwordTxtBx = password.getText().toString();
final FirebaseFirestore database = FirebaseFirestore.getInstance();
final DocumentReference usernameDocument = database.document("mylist/" + usernameTxtBx);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
usernameDocument.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot document = task.getResult();
if(document.exists()){
Toast.makeText(LoginActivity.this, "User exist", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "No user", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Some error", Toast.LENGTH_SHORT).show();
}
}
});
}
});
【问题讨论】:
-
如果你尝试打印
mylist/" + usernameTxtBx,结果如何? -
将默认值添加到
usernameTxtBx,如“0”或其他,因为空值不能是文档ID。
标签: android firebase google-cloud-firestore