0

I'm working on an application that sends a polygon to firestore, and then retries it later. The problem is at it's written in the title. Polygon doesn't have a empty constructor, for the polygon.

I have tried to look at other solutions, but non fitting my problem, with the polygons. Some talks about creating ones own no-argument constructor, but not explaining it in a way that I understand. (Have to be almost at the ELI5 level on reddit)

Non of the things that I have tried, seemed to work, and its frustrating. There is no problem with uploading the information to firebase, only pulling it down.

---------------The code for fetching the collection --------------

private void getFlightZonesRef(){
        CollectionReference mFlightZonesRef = mDb
                .collection(getString(R.string.collection_flight_zones));

        mFlightZonesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for (QueryDocumentSnapshot fZ : task.getResult()){

                        Log.d(TAG, "onComplete: TESTING: " + fZ.getData());

                        flyZonesModel = fZ.toObject(FlyZonesModel.class);
                        Log.d(TAG, "onComplete: REFFlyZone: " + flyZonesModel.toString());
                    }
                }else Toast.makeText(getContext(), "Something went wrong \n getting data from FireBase", Toast.LENGTH_SHORT).show();
            }
        });

    }

---------------------Saving the information on firebase -----------

DocumentReference newPolygonRef = mDb
                                .collection(getString(R.string.collection_flight_zones))
                                .document();
                        newPolygonRef.set(flyZonesModel).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()){
                                    Toast.makeText(getContext(), "Zone, succesfully added to database", Toast.LENGTH_SHORT).show();
                                    mNameOfArea.setText("");
                                    mDescriptionOfArea.setText("");
                                    if (polygon != null){
                                        polygon.remove();
                                        polygon = null;
                                    }
                                }else{
                                    Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
                                    Log.d(TAG, "onComplete: Faliure:" + task.getException());
                                }

------------------polygon information model ------------------

package com.bach.droneinfo.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.gms.maps.model.Polygon;

public class FlyZonesModel implements Parcelable {

    private String name;
    private String description;
    private Polygon zone;

    public FlyZonesModel(Polygon zone, String name, String description) {
        this.name = name;
        this.description = description;
        this.zone = zone;
    }

    public FlyZonesModel() {
    }
¨¨¨¨¨
more code
¨¨¨¨¨

public Polygon getZone() {
        return zone;
    }

    public void setZone(Polygon zone) {
        this.zone = zone;
    }

----------------- ERROR CODE ----------------------------------

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bach.droneinfo, PID: 11682
    java.lang.RuntimeException: Could not deserialize object. Class com.google.android.gms.maps.model.Polygon does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped (found in field 'zone')
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(com.google.firebase:firebase-firestore@@18.2.0:539)
        at com.google.firebase.firestore.util.CustomClassMapper.access$200(com.google.firebase:firebase-firestore@@18.2.0:53)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.2.0:695)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-firestore@@18.2.0:689)
        at com.google.firebase.firestore.util.CustomClassMapper.convertBean(com.google.firebase:firebase-firestore@@18.2.0:518)
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-firestore@@18.2.0:248)
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeToType(com.google.firebase:firebase-firestore@@18.2.0:186)

I wanted to have a polygon that could de displayed on the map (GoogleMap) but get the error code instead.

have no idea on how to place a polygon on the map without using

mMap.addPolygon(polygonOptions);


First post, so if there is also some input on how to post correctly, some pointers would be gladly taken.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You can't just add arbitrary Java objects into Firestore. Looks like Polygon isn't going to work for you, since it's not a JavaBean type object. Come up with your own object model and copy data into it instead. Or insert data via a Map object. – Doug Stevenson May 03 '19 at 15:26
  • @Dough Stevenson Could you elaborate? As said there is no problem placing the the polygon into Firestore. I get all the data placed into firestore as it is written in my code, everything from points to such things as stroke width. – Drunkduckling May 03 '19 at 15:57
  • I should rephrase - you can't just **read** arbitrary objects. Polygon isn't going to work because it doesn't define a no-arg constructor that the SDK can use to create a new instance of the object. Without that, the SDK doesn't know to make a new Polygon object. This is what the error message is trying to tell you. – Doug Stevenson May 03 '19 at 16:11

0 Answers0