简体   繁体   中英

argument 1 has type java.util.TreeMap, got java.util.HashMap

I am Putting the data in Firebase Document as

try {
                String uid = user.getUid();
                String title  = InputProductTitle.getText().toString();
                Double price = Double.valueOf(InputProductPrice.getText().toString());
                String categories = CategoryDataFromPreviousIntent.split("&&")[0];
                String subCategories = CategoryDataFromPreviousIntent.split("&&")[1];
                String dateAdded = new Date().toString();
                String description = InputProductDescription.getText().toString();
                Post newPost = new Post(uid,title, price, categories, subCategories, dateAdded, false,description);

                mFirebase.collection("NewPost").document().set(newPost).addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(getApplicationContext(), "Added", Toast.LENGTH_SHORT).show();
                    }

                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getApplicationContext(), "Error" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }

Where the Post is a class as Follows

public class Post {
private String addedBy;
private String title;
private Double price;
private String categories;
private String subCategories;
private TreeMap<String,Double> bids;
private String dateAdded;
private boolean status;
private String imageLink;
private String soldTo;
private String description;
public Post(){}

public Post(String addedBy, String title ,Double price, String categories, String subCategories, String dateAdded, boolean status, String description) {
    this.addedBy = addedBy;
    this.title = title;
    this.price = price;
    this.categories = categories;
    this.subCategories = subCategories;
    this.dateAdded = dateAdded;
    this.status = status;
    this.imageLink = "";
    this.soldTo = "";
    this.description = description;
    this.bids = new TreeMap<String, Double>();
}

I have declared the bids to be of Type TreeMap. but when i am trying to retrieve the data back from Firebase in my application like this

private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("NewPost");
public void loadNotes() {
    notebookRef.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {

                        Map<String, Object> allData = documentSnapshot.getData();
                        Post obj = documentSnapshot.toObject(Post.class);


                }
            });
}

I get the following error in the line Post obj = documentSnapshot.toObject(Post.class);

saying that

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ems.erp.mmdu.com.forfirebasestorage, PID: 31905
java.lang.IllegalArgumentException: method ems.erp.mmdu.com.forfirebasestorage.Post.setBids argument 1 has type java.util.TreeMap, got java.util.HashMap
    at java.lang.reflect.Method.invoke(Native Method)
    at com.google.firebase.firestore.util.ApiUtil.invoke(com.google.firebase:firebase-firestore@@18.0.1:61)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:702)
    at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.0.1:675)
    at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.0.1:504)
    at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.0.1:243)
    at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-firestore@@18.0.1:97)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:203)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:121)
    at com.google.firebase.firestore.DocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:183)
    at com.google.firebase.firestore.QueryDocumentSnapshot.toObject(com.google.firebase:firebase-firestore@@18.0.1:101)
    at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:105)
    at ems.erp.mmdu.com.forfirebasestorage.AmmarFragment$1.onSuccess(AmmarFragment.java:98)
    at com.google.android.gms.tasks.zzn.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:200)
    at android.app.ActivityThread.main(ActivityThread.java:6956)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:519)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836)

I am nowwhere putting bids of Post class as HashMap, I am always using TreeMap but then now when the data from Firebase is coming it comes as HashMap why is this happening.

I tried converting the TreeMap to HashMap everywhere it is working fine but then i want to use TreeMap.

Can anyone Explain me Please. Thank you.

将其声明为Map,然后尝试使用该map作为构造函数的参数创建一个树map对象

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM