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

标签 java android firebase google-cloud-firestore

这个问题在这里已经有了答案:





Error in fetching data from Firebase to RecyclerView. FATAL EXCEPTION: ... does not define a no-argument constructor

(1 个回答)


2年前关闭。




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

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 this shows in logcat. I don't know how to fix it.



我的代码 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 + "\n\n" ;
                    }
                    getinfo.setText(date);
                }
            });
}

}

成员类:
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;
    }
}

关于java - 无法反序列化对象。没有定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60389906/

相关文章:

java - 如何在 Eclipse 中启动 Java EE 6 项目

java - native 查询参数在 Java EE 中不起作用

java - JFrame 协助

android - 如何从 Couchbase 数据库中保存和获取数据并将其显示在 ListView 上?

javascript - 如何从 Google Firebase 函数中失败的 Promise 返回自定义错误消息?

java - 操作非静态变量

Android 使用 Facebook Sdk 3.1 邀请好友加入 App(打开 Play 商店链接)

android - Nexus S - 访问\修改驱动程序

java - 从 Cloud Firestore 检索复杂文档

javascript - firebase child_added 事件问题