androiddictionaryfirebase-realtime-databasegeofirecurrentlocation

How to fix " java.lang.NoSuchMethodError: com.google.firebase.database.DatabaseReference.setValue"


I'm setting up a current location in my application.Where do i need to set the encoding?

    @Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

    //Geo Fire for current location
    String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
    DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");

    GeoFire geoFireAvailable = new GeoFire(refAvailable);
    GeoFire geoFireWorking = new GeoFire(refWorking);

    switch (customerId){
        case "":

            geoFireWorking.removeLocation(userId);
            geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {

                }
            });

            break;
            default:
                geoFireAvailable.removeLocation(userId);
                geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
                    @Override
                    public void onComplete(String key, DatabaseError error) {

                    }
                });

                break;
    }


}

Following is error which I'm getting:

java.lang.NoSuchMethodError: com.google.firebase.database.DatabaseReference.setValue at com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:215) at com.firebase.geofire.GeoFire.removeLocation(GeoFire.java:192) at com.example.webforest.quickaidlikeuber2nd.DriverMapActivity.onLocationChanged(DriverMapActivity.java:184)


Solution

  • This bug appears to manifest automatically when you are using setLocation() method without the use of GeoFire.CompletionListener(). If you implement the CompletionListener, this should resolve the issue.

    Therefore, replace

    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
    

    with

    geoFire.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener(){ });
    

    Edit 2:
    You may need to adapt to a different approach if you are still encountering difficulties.

    Replace the following code:

    geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
        @Override
        public void onComplete(String key, DatabaseError error) {
    
        }
    });
    

    With the following code:

    GeoHash geoHash = new GeoHash(new GeoLocation(location.getLatitude(),location.getLongitude()));
    Map<String, Object> updates = new HashMap<>();
    updates.put("g", geoHash.getGeoHashString());
    updates.put("l", Arrays.asList(location.getLatitude(),location.getLongitude()));
    geoFireAvailable.setValue(updates,geoHash.getGeoHashString());