检查Firebase数据库集合中是否存在文档名称

时间:2019-11-13 19:42:37

标签: java firebase google-cloud-firestore firebase-authentication

我希望用户通过文档名称/ ID登录到我的应用程序。当他们输入并单击登录时,我要搜索文档名称是否存在于集合中。以下代码是在提交时强制关闭应用程序!

请检查我在做什么错,如果可能,请发布固定代码。

package net.smallacademy.authenticatorapp;
import...

public class Login extends AppCompatActivity {
EditText mKey;
Button mLoginBtn;
ProgressBar progressBar;
FirebaseAuth fAuth;
FirebaseFirestore fStore;

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


    mKey = findViewById(R.id.key);
    progressBar = findViewById(R.id.progressBar);
    fAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();
    mLoginBtn = findViewById(R.id.loginBtn);

    mLoginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            String key = mKey.getText().toString().trim();


            if(TextUtils.isEmpty(key)){
                mKey.setError("Key is Required.");
                return;
            }
            if(key.length() < 6){
                mKey.setError("Key Must be >= 6 Characters");
                return;
            }
            progressBar.setVisibility(View.VISIBLE);


            DocumentReference documentReference = fStore.collection("members").document(String.valueOf (mKey));
            documentReference.addSnapshotListener(new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(DocumentSnapshot snap, FirebaseFirestoreException fe) {
                    if (snap.exists ( )) {
                        Toast.makeText(Login.this, "Logged in Successfully", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(getApplicationContext(),MainActivity.class));
                    } else {
                        Toast.makeText(Login.this, "Error ! ", Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.GONE);
                    }

                }



                });

        }

    });

    }





}

LOGCAT: enter image description here

enter image description here

enter image description here

0 个答案:

没有答案