2 years ago

#59841

test-img

alien_killer

How to solve the error "java.lang.AssertionError: Got Datasnapshot without location with key You" in Android Studio with Java

The application aims to track the user location and encrypt the location and save the location into firebase by using geofire. Geofence was also being used in this application to set an area as the restricted area.

The following code is used to set the user location into firebase by using geofire:

private void settingGeoFire() {
    String firebaseAuth = FirebaseAuth.getInstance().getUid();
    myLocationRef = FirebaseDatabase.getInstance().getReference("User_Location/"+firebaseAuth);
    myLocationRef.child("userID").setValue(firebaseAuth);
    geoFire = new GeoFire(myLocationRef);

}

The following code is used to retrieve the location from firebase, encrypt and send it back to firebase:

private void getLocationFromFirebaseAndEncrypt(){

    String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    location = FirebaseDatabase.getInstance().getReference("User_Location").child(userID).child("You").child("l");

    location.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            try {
                if (dataSnapshot.getValue() != null) {
                    Double d1 = (Double) dataSnapshot.child("0").getValue();
                    Double d2 = (Double) dataSnapshot.child("1").getValue();

                    String latitude = String.valueOf(d1);
                    String longitude = String.valueOf(d2);

                    Log.d("LatLong", latitude + ", " + longitude); //Check the values


                    //encrypt
                    LocationEncryption locationEncryption = new LocationEncryption();
                    String encryptedLatitude = null;
                    String encryptedLongitude = null;

                    encryptedLatitude = locationEncryption.encrypt(latitude);
                    encryptedLongitude = locationEncryption.encrypt(longitude);

                    uploadDataToFirebase(location, encryptedLatitude, encryptedLongitude);

                    Log.d("Encrypted", encryptedLatitude + ", " + encryptedLongitude); //Check the values








                }
                else {
                    Toast.makeText(getBaseContext(), "NULL", Toast.LENGTH_SHORT).show();
                }

            }catch (Exception e)
            {
                e.printStackTrace();
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }


    });

}

private void uploadDataToFirebase(DatabaseReference location, String encryptedLatitude, String encryptedLongitude) {

    EncryptedData encryptedData = new EncryptedData(encryptedLatitude, encryptedLongitude);
    location.child("0").setValue(encryptedLatitude);
    location.child("1").setValue(encryptedLongitude);


}

The following code is used to set up geofence and geoquery when user get into the geofencing area:

private void addCircleArea() {
    if(geoQuery != null){
        geoQuery.removeGeoQueryEventListener(this);
        geoQuery.removeAllListeners();
    }

    for(LatLng latLng : dangerousArea){
        mMap.addCircle(new CircleOptions().center(latLng).radius(100).strokeColor(Color.RED).fillColor(Color.rgb(255,200,200)).strokeWidth(1.0f));

        //create GeoQuery when user in dangerous area
        geoQuery = geoFire.queryAtLocation(new GeoLocation(latLng.latitude, latLng.longitude), 0.1f);
        geoQuery.addGeoQueryEventListener(MapsActivity.this);

    }

}

When the user's current location is outside the geofence (outside the red circle on the map), the code runs smoothly without any error. But when the user's location is inside a geofence area (red circle on the map), the app crashed and an error occurs. The error is shown below:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: assignment.yeo.location, PID: 27523
java.lang.AssertionError: Got Datasnapshot without location with key You
    at com.firebase.geofire.GeoQuery.childAdded(GeoQuery.java:275)
    at com.firebase.geofire.GeoQuery.access$000(GeoQuery.java:41)
    at com.firebase.geofire.GeoQuery$1.onChildAdded(GeoQuery.java:62)
    at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:79)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Any solution for it? Thank you very much.

java

android

firebase

geofire

0 Answers

Your Answer

Accepted video resources