Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not deserialize object. does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped [duplicate]

I can update to Firestore but I can't retrieve data from my document. I think the mainly problem is

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.

my code 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);
                }
            });
}

}

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;
}

}

complete 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)
like image 387
bean Avatar asked Feb 25 '20 07:02

bean


People also ask

Could not deserialize an object does not define a no-argument constructor?

The error is very clear, your class "FireFran" doesn't have a no-argument constructor. When you try to deserialize an object from Cloud Firestore, the Android SDKs require that the class must have a default no-arg constructor and also setters that map to each database property.

Does not define a no-argument constructor If you are using ProGuard make sure?

Donor does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.


1 Answers

Add no-argument constructor to 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;
    }
}
like image 182
Royal Tiger Avatar answered Sep 22 '22 21:09

Royal Tiger