无法反序列化对象.没有定义无参数构造函数.如果您正在使用ProGuard,请确保这些构造函数没有被剥离



我可以更新到Firestore,但无法从文档中检索数据。我认为主要的问题是

无法反序列化对象。类com.example.dotdot.Member没有定义无参数构造函数。如果您正在使用ProGuard,请确保这些构造函数没有被剥离这显示在logcat中。我不知道怎么修。

我的代码test.java:

public class test extends AppCompatActivity {
private EditText nameinput;
private EditText passwordinput;
private EditText phoneinput;
private TextView getinfo;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private DocumentReference accountRef = db.collection("Member").document();
private CollectionReference memRef = db.collection("Member");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
nameinput = findViewById(R.id.name);
passwordinput = findViewById(R.id.password);
phoneinput = findViewById(R.id.phone);
getinfo = findViewById(R.id.getinfo);
}
public void insertAccount(View v){
String name = nameinput.getText().toString();
String password = passwordinput.getText().toString();
String phone = phoneinput.getText().toString();
Member account = new Member(name,password,phone);
memRef.add(account);
}
public void getAccount(View v){
memRef.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {//test.jave:85
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
String date = "";
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
Member mem = documentSnapshot.toObject(Member.class); //test.jave:90
String name = mem.getName();
String password = mem.getPassword();
String phone = mem.getPhone();
date += "name:" + name + "npassword:" + password + "nphone:" + phone + "nn" ;
}
getinfo.setText(date);
}
});
}

}

Member.class:

public class Member {
private String name;
private String phone;
private String password;
public Member(String name,String password,String phone){
this.name = name;
this.phone = phone;
this.password = password;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getPassword() {
return password;
}

}

完整logcat:

2020-02-25 15:43:19.055 4313-4313/com.example.dotdot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dotdot, PID: 4313
java.lang.RuntimeException: Could not deserialize object. Class com.example.dotdot.Member does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(com.google.firebase:firebase-firestore@@21.4.0:563)
at com.google.firebase.firestore.util.CustomClassMapper.access$200(com.google.firebase:firebase-firestore@@21.4.0:54)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@21.4.0:749)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@21.4.0:741)
at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@21.4.0:542)
at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@21.4.0:253)
at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@21.4.0:100)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.0:210)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.0:116)
at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.0:188)
at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@21.4.0:97)
at com.example.dotdot.test$1.onSuccess(test.java:90)
at com.example.dotdot.test$1.onSuccess(test.java:85)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Member添加无参数构造函数

public class Member {
private String name;
private String phone;
private String password;
public Member(String name,String password,String phone){
this.name = name;
this.phone = phone;
this.password = password;
}
//Add this
public Member() {
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String getPassword() {
return password;
}
}

最新更新