I am creating an app like Uber but I have ran into a problem after I got an error suddenly and the app crashed. I tried out many ways in order to get ride of this error but it didn't work. All the answers to the similar questions that I could find on Stackoverflow were that this problem is caused due to FirebaseDatabase.getInstance().setPersistenceEnabled(true)
and I had to delete this code of line as this is a bug with Firebase that is yet to be solved. But this didn't solve my problem as i didn't have any such code. That's why i thought this is a question which is not on stackoverflow and asked the question. This is my Java Code
package com.matt.autozauser;
import com.directions.route.AbstractRouting;
import com.directions.route.Route;
import com.directions.route.RouteException;
import com.directions.route.Routing;
import com.directions.route.RoutingListener;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.firebase.geofire.GeoQuery;
import com.firebase.geofire.GeoQueryEventListener;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.ui.PlaceAutocomplete;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Welcome extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener{
SupportMapFragment mapFragment;
private Button btnBooking;
private Location lastLocation;
private GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener;
private LocationRequest locationRequest;
private LocationListener locationListener;
private LocationManager locationManager;
private Marker marker, driverMarker;
private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private final int RequestCode = 10;
private final int ResourceCode = 11;
private DatabaseReference userLastLocation, customersUnderServiceRef, userRequest, driversOnDuty,workingDrivers, driverRef1, driverWorkingRef ;
GeoFire location, onDuty, customersUnderService;
private Boolean clicked = false;
private String driverID = "";
private ValueEventListener driverListener;
private GeoQuery geoQuery;
private String myId = "";
private Double driverLat, driverLng;
private PlaceAutocomplete pickup, drop;
@Override
protected void onCreate(Bundle savedInstanceState)
{
checkLocationPermission();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
userLastLocation = FirebaseDatabase.getInstance().getReference("User LastLocation");
location = new GeoFire(userLastLocation);
setupLocation();
driversOnDuty = FirebaseDatabase.getInstance().getReference("DriversOnDuty");
onDuty = new GeoFire(driversOnDuty);
driverWorkingRef = FirebaseDatabase.getInstance().getReference().child("DriversWorking");
customersUnderServiceRef = FirebaseDatabase.getInstance().getReference().child("CustomersUnderService");
customersUnderService = new GeoFire(customersUnderServiceRef);
}
@Override
public void onMapReady(GoogleMap googleMap)
{
myId = FirebaseAuth.getInstance().getCurrentUser().getUid();
setupUiViews();
mMap = googleMap;
displayLocation();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
switch (requestCode) {
case RequestCode:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (checkPlayServices()){
buildGoogleApiClient();
createLocationRequest();
displayLocation();
}
}
break;
}
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
if (googleApiClient == null){
buildGoogleApiClient();
}
if (!googleApiClient.isConnected()){
googleApiClient.connect();
}
startLocationUpdates();
displayLocation2();
locationRequest = LocationRequest
.create()
.setInterval(1000)
.setFastestInterval(500)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
startLocationUpdates();
displayLocation();
}else {
checkLocationPermission();
}
}
@Override
public void onConnectionSuspended(int i)
{
googleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
}
@Override
public void onLocationChanged(Location location)
{
lastLocation = location;
displayLocation2();
}
public void checkLocationPermission()
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RequestCode);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RequestCode);
}
}
}
protected synchronized void buildGoogleApiClient()
{
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
private void setupUiViews()
{
btnBooking = findViewById(R.id.bookingButton);
btnBooking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (clicked == false) {
Toast.makeText(Welcome.this, "Getting Cab", Toast.LENGTH_SHORT).show();
btnBooking.setText(R.string.gettingCab);
clicked = true;
getNearestDriver();
} else
{
if(!driverFound){
btnBooking.setText(R.string.callTaxi);
stopLocationUpdates();
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference databaseReference1 = FirebaseDatabase.getInstance().getReference("Customer Request");
GeoFire geoFire1 = new GeoFire(databaseReference1);
geoFire1.removeLocation(userId);
Toast.makeText(Welcome.this, "Canceling Cab", Toast.LENGTH_SHORT).show();
if(driverMarker!=null){
driverMarker.remove();
}
clicked = false;
}
}
}
});
}
private void displayLocation()
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastLocation != null ){
final Double lat = lastLocation.getLatitude();
final Double lng = lastLocation.getLongitude();
location.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(lat, lng), new GeoFire.CompletionListener()
{
@Override
public void onComplete(String key, DatabaseError error) {
if (marker != null) {
marker.remove();
marker = mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15.0f));
}else{
marker = mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 15.0f));
}
}
});
}else{
Log.d("Error", "Cannot Get Your Location");
}
}
private void displayLocation2()
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (lastLocation != null ){
if(clicked == true){
if (marker != null){
marker.remove();
}
final Double lat = lastLocation.getLatitude();
final Double lng = lastLocation.getLongitude();
customersUnderService.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(lat, lng), new GeoFire.CompletionListener()
{
@Override
public void onComplete(String key, DatabaseError error) {
if (marker != null) {
marker.remove();
marker = mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)).title("You"));
}else{
marker = mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker)).title("You"));
}
}
});
}
}else{
Log.d("Error", "Cannot Get Your Location");
}
}
private void setupLocation()
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RequestCode);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RequestCode);
}
}else{
if (checkPlayServices())
{
buildGoogleApiClient();
createLocationRequest();
}
}
}
private void createLocationRequest()
{
locationRequest = LocationRequest.create()
.setInterval(1500)
.setFastestInterval(500)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setSmallestDisplacement(0);
}
private boolean checkPlayServices()
{
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS)
{
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
GooglePlayServicesUtil.getErrorDialog(resultCode, this, ResourceCode).show();
else {
Toast.makeText(this, "This Device Is Not Supported", Toast.LENGTH_SHORT).show();
finish();
}return false;
}
return true;
}
private void stopLocationUpdates()
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
private void startLocationUpdates()
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
private int radius = 1;
private Boolean driverFound = false;
private void getNearestDriver()
{
geoQuery = onDuty.queryAtLocation(new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()), radius);
geoQuery.removeAllListeners();
geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
String driverId = key;
if(!driverFound){
driverFound = true;
driverID = key;
DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("DriversWorking").child(driverId);
String customerId = FirebaseAuth.getInstance().getCurrentUser().getUid();
HashMap map = new HashMap();
map.put("customerRideId", customerId);
driverRef.updateChildren(map);
driverID = key;
btnBooking.setText(R.string.gettingTaxiLocation);
customersUnderService.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));
getDriverLocation();
driversOnDuty.child(key).removeValue();
}
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) { }
@Override
public void onGeoQueryReady() {
if(!driverFound){
radius++;
getNearestDriver();
}
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
private void getDriverLocation()
{
driverWorkingRef = driverWorkingRef.child(driverID).child("l");
driverListener = driverWorkingRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
List<Object> map = (List<Object>) dataSnapshot.getValue();
double locationLat = 0;
double locationLng = 0;
btnBooking.setText(R.string.driverComing);
if (map.get(0) != null)
{
locationLat = Double.parseDouble(map.get(0).toString());
driverLat = locationLat;
}
if (map.get(1) != null)
{
locationLng = Double.parseDouble(map.get(1).toString());
driverLng = locationLng;
}
LatLng driverLatLng = new LatLng(locationLat, locationLng);
if (driverMarker != null) {
driverMarker.remove();
}
driverMarker = mMap.addMarker(new MarkerOptions().position(driverLatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.carmarker)).title("You"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(driverLatLng, 15.0f));
Location myLoc = new Location("");
Location driverLoc = new Location("");
myLoc.setLatitude(lastLocation.getLatitude());
myLoc.setLongitude(lastLocation.getLongitude());
driverLoc.setLatitude(locationLat);
driverLoc.setLongitude(locationLng);
float distance = myLoc.distanceTo(driverLoc);
;
if (distance<100){
btnBooking.setText(R.string.driverReached);
}
}getDriverLocation();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onStop()
{
super.onStop();
}
@Override
protected void onPause() {
super.onPause();
}
}
These are the errors in the Logcat.
06-04 17:39:10.550 13127-13127/com.matt.autozauser E/art: The String#value field is not present on Android versions >= 6.0
06-04 17:39:11.166 13127-13358/com.matt.autozauser E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
PATH2 /vendor/lib64/hw/gralloc.qcom.so
PATH1 /system/lib64/hw/gralloc.qcom.so
PATH3 /odm/lib64/hw/gralloc.msm8953.so
PATH2 /vendor/lib64/hw/gralloc.msm8953.so
PATH1 /system/lib64/hw/gralloc.msm8953.so
06-04 17:40:19.271 13127-13228/com.matt.autozauser E/NoopPersistenceManager: Caught Throwable.
java.lang.StackOverflowError: stack size 1037KB
at com.google.firebase.database.collection.zza.insert(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzgj.zzb...[20]
You can add the following line in AndroidManifest.xml
:
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With