无法将数据写入数据库Firebase Firestore

时间:2020-04-02 07:46:34

标签: android google-cloud-firestore

我设法将数据插入身份验证,但无法插入数据库:

代码

fullname = findViewById(R.id.Ifullname);
        password = findViewById(R.id.Ipassword);
        email = findViewById(R.id.Iemail);
        RegisterBtn = findViewById(R.id.button);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();
        loginBtn = findViewById(R.id.LoginBtn);
RegisterBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String FullName = fullname.getText().toString().trim();
                String Password = password.getText().toString().trim();
                String Email = email.getText().toString().trim();
fAuth.createUserWithEmailAndPassword(Email, Password).addOnCompleteListener(new OnCompleteListener<AuthResult>(){
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task){
                        if(task.isSuccessful()){
                            Toast.makeText(Register.this, "User Created.", Toast.LENGTH_SHORT).show();
                            UserID = fAuth.getCurrentUser().getUid();
                            DocumentReference documentReference = fStore.collection("users").document(UserID);
                            Map<String,Object> user = new HashMap<>();
                            user.put("FName", fullname);
                            user.put("Email",email);
                            user.put("Password",password);
//problem seems to be this line  
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Log.d(TAG, "onSuccess: user Profile is created");
                                }

                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Log.d(TAG, "onFailure:"+e.toString());

                                }
                            });

错误

E / AndroidRuntime:致命异常:主要过程: com.example.budgetingapp,PID:11507 java.lang.RuntimeException:找到 类上的名称getText的获取器冲突 androidx.appcompat.widget.AppCompatEditText位于 com.google.firebase.firestore.util.CustomClassMapper $ BeanMapper。(com.google.firebase:firebase-firestore @@ 21.4.1:629) 在com.google.firebase.firestore.util.CustomClassMapper.loadOrCreateBeanMapperForClass(com.google.firebase:firebase-firestore @@ 21.4.1:377) 在com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore @@ 21.4.1:177) 在com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore @@ 21.4.1:140) 在com.google.firebase.firestore.util.CustomClassMapper.serialize(com.google.firebase:firebase-firestore @@ 21.4.1:104) 在com.google.firebase.firestore.util.CustomClassMapper.convertToPlainJavaTypes(com.google.firebase:firebase-firestore @@ 21.4.1:78) 在com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(com.google.firebase:firebase-firestore @@ 21.4.1:231) 在com.google.firebase.firestore.UserDataReader.parseSetData(com.google.firebase:firebase-firestore @@ 21.4.1:75) com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore @@ 21.4.1:166) com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore @@ 21.4.1:146) at com.example.budgetingapp.Register $ 1 $ 1.onComplete(Register.java:95) 位于com.google.android.gms.tasks.zzj.run(未知来源:4) 在android.os.Handler.handleCallback(Handler.java:873) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:1)

简而言之,您正在尝试将TextField本身添加到数据库中,而不是添加到您创建的字符串中。

LazyLoadedComponent

我建议采用一些命名约定来防止这种情况。我个人建议用String FullName = fullname.getText().toString().trim(); String Password = password.getText().toString().trim(); String Email = email.getText().toString().trim(); /* ... */ user.put("FName", fullname); user.put("Email",email); user.put("Password",password); 编写的<short-object-type><variable-name>

camelCase

有时,将私有对象级变量与局部变量区分开来也很有用,通常是在名称的开头添加Button #button -> #btnRegister Button #LoginBtn -> #btnLogin TextField #fullname -> #txtFullName TextField #email -> #txtEmail TextField #password -> #txtPwd (对我来说):

m

您应该在mBtnRegister.setOnClickListener(...); Button btnScripted = new Button(...); 中为变量命名(对于常量,请在camelCase中命名,而不是在UPPER_SNAKE_CASE中命名,这样就不会将它们误识别为类,接口等。实际上,您可以在通过查看StackOverflow代码块的语法高亮显示操作。


PascalCase