向Firestore发送电子邮件时出现不支持的类型错误



我在将数据(姓名、姓氏、电子邮件(保存到FireStore数据库时遇到问题。授权运行良好,正在添加新用户。但我想把AndroidStudio的EditText类型中的这3个变量保存为Firebase的新文档。但控制台中突然出现错误:

Process: com.example.poduszkowoapp, PID: 7276
java.lang.IllegalArgumentException: Invalid data. Unsupported type: androidx.appcompat.widget.AppCompatEditText (found in field Email)
at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@17.1.2:293)
at com.google.firebase.firestore.UserDataConverter.parseScalarValue(com.google.firebase:firebase-firestore@@17.1.2:405)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@17.1.2:254)
at com.google.firebase.firestore.UserDataConverter.parseMap(com.google.firebase:firebase-firestore@@17.1.2:274)
at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@17.1.2:230)
at com.google.firebase.firestore.UserDataConverter.parseSetData(com.google.firebase:firebase-firestore@@17.1.2:75)
at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@17.1.2:172)
at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@17.1.2:152)
at com.example.poduszkowoapp.RegisterActivity$1$2.onComplete(RegisterActivity.java:90)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

出现问题的注册活动中本节的代码:

fAuth.createUserWithEmailAndPassword(txtEmail.getText().toString(),txtPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful())
{
userID = fAuth.getCurrentUser().getUid();
Map<String, Object> userMap = new HashMap<>();
userMap.put("Name", txtName);
userMap.put("Surname", txtSurname);
userMap.put("Email", txtEmail);
fStore.collection("users").document(userID).set(userMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(RegisterActivity.this, "Registered succesfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
});
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if(e instanceof FirebaseAuthUserCollisionException)
{
Toast.makeText(RegisterActivity.this, "This email address is already taken", Toast.LENGTH_LONG).show();
}
}
});

看起来您正在将Android视图的实例传递给Firestore:

Map<String, Object> userMap = new HashMap<>();
userMap.put("Name", txtName);
userMap.put("Surname", txtSurname);
userMap.put("Email", txtEmail);

如果txtEmail和其他对象是Android视图对象,那么这根本不起作用。Firestore对Android视图一无所知。您必须编写代码从小部件中提取字符串值,并将这些字符串传递给Firestore。例如,要从EditText:中获取字符串

userMap.put("Email", txtEmail.getText().toString());

让你的地图是:

Map<String, Object> userMap = new HashMap<>();
userMap.put("Name", txtName.getText().toString());
userMap.put("Surname", txtSurname.getText().toString());
userMap.put("Email", txtEmail.getText().toString());

您正在将您的EditText类型写入firestore,而不是其中的文本。

最新更新