0

I am making an app like uber and i have to put driver location with marker on app and after sign in it shows loading and then crashes? i have imported all libraries properly but my logcat screen i have tried going to similar questions on stackOverflow but nothing seems working and this bug just shows again and again and i am not unable to start my app . Welcome Activity

{


private GoogleMap mMap;

//Play Services
private static final int MY_PERMISSION_REQUEST_CODE=7000;
private static final int PLAY_SERVICE_RES_REQUEST= 7001;


private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;


private static int UPDATE_INTERVAL = 5000;
private static int FATEST_INTERVAL = 3000;
private static int DISPLACEMENT = 10;

DatabaseReference drivers;
GeoFire geoFire;
Marker mCurrent;

MaterialAnimatedSwitch location_switch;
SupportMapFragment mapFragment;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //Init View
    location_switch = (MaterialAnimatedSwitch)findViewById(R.id.location_switch);
    location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(boolean isOnline) {
            if (isOnline)
            {
                startLocationUpdates();
                displayLocation();
                Snackbar.make(mapFragment.getView(),"You are Online",Snackbar.LENGTH_SHORT)
                        .show();
            }
            else
            {
                stopLocationUpdates();
                mCurrent.remove();
                Snackbar.make(mapFragment.getView(),"You are Offline",Snackbar.LENGTH_SHORT)
                        .show();
            }


        }
    });

    //Geo Fire
    drivers = FirebaseDatabase.getInstance().getReference("Drivers");
    geoFire = new GeoFire(drivers);

    setUpLocation();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case MY_PERMISSION_REQUEST_CODE:
            if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                if (checkPlayServices())
                {
                    buildGoogleApiClient();
                    createLocationRequest();
                    if(location_switch.isChecked())
                        displayLocation();
                }
            }
    }
}

private void setUpLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
    {
        //Request runtime Permission
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        },MY_PERMISSION_REQUEST_CODE);
    }
    else
    {
        if (checkPlayServices())
        {
            buildGoogleApiClient();
            createLocationRequest();
            if(location_switch.isChecked())
                displayLocation();
        }
    }
}

private void createLocationRequest()  {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FATEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);

}

private void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if(resultCode != ConnectionResult.SUCCESS)
    {
        if(GooglePlayServicesUtil.isUserRecoverableError(resultCode))
            GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICE_RES_REQUEST).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_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
    {
        return;
    }

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);

}

private void displayLocation() {
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
    {
        return;
    }

    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null)
    {
        if (location_switch.isChecked())
        {
            final double latitude = mLastLocation.getLatitude();
            final double longitude = mLastLocation.getLongitude();

            //Update to Firebase
            geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {
                    //Add Marker
                    if (mCurrent != null)
                        mCurrent.remove(); //Remove already Marker
                    mCurrent = mMap.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.lender))
                            .position(new LatLng(latitude,latitude))
                            .title("You"));

                    //Move the camera to this position
                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),15.0f));
                    //Draw Animation Rotate Marker
                    rotateMarker(mCurrent,-360,mMap);

                }
            });
        }
    }
    else
    {
        Log.d("ERROR","Cannot Get Your Location");
    }
}

private void rotateMarker(final Marker mCurrent, final float i, GoogleMap mMap) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    final float startRotation = mCurrent.getRotation();
    final long duration = 1500;


    final Interpolator interpolator = new LinearInterpolator();
    handler.post(new Runnable() {
        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start ;
            float t = interpolator.getInterpolation((float)elapsed/duration);
            float rot = t*i+(1-t)*startRotation;
            mCurrent.setRotation(-rot > 180?rot/2:rot);

            if(t<1.0)
            {
                handler.postDelayed(this,16);
            }
        }
    });
}

private void startLocationUpdates() {

    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED )
    {
        return;
    }

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;



}

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;
    displayLocation();

}

@Override
public void onConnected(@Nullable Bundle bundle) {

    displayLocation();
    startLocationUpdates();

}

@Override
public void onConnectionSuspended(int i) {

    mGoogleApiClient.connect();

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Laman Ansari
  • 39
  • 1
  • 7
  • 1
    You should check out [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) and add the relevant part of the stack trace here. – Markus Kauppinen Jan 10 '19 at 09:07
  • I have tried to look into stack trace but not able to get the problem , please help me . I have already attached the logcat screen in my question. – Laman Ansari Jan 10 '19 at 10:08
  • can you at least post error that is in logcat ? – Stavro Xhardha Jan 10 '19 at 13:08
  • I don't know specifically how to detect error in logcat how , as everything in logcat is in red and it is very lengthy – Laman Ansari Jan 10 '19 at 16:18
  • It is already given in the question and you can get it by clicking the hyperlink LogcatScreen in question. – Laman Ansari Jan 10 '19 at 16:20
  • There's a problem in the layout you're passing to `setContentView()` in the `Welcome` `Activity`'s `onCreate()` method. Looks like you've got an invalid tag name there somewhere. – Mike M. Jan 10 '19 at 21:00

0 Answers0