I am new in android development and creating an app to place my live location on map...but I don't know why this application keeps stopping. I think this is due to the locationManager because when I commented that part..then the app didn't stop.
please help me regarding this error.....
Below is the code for the project>>
package com.example.thefinalapplication;
import ...
import java.util.Arrays;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private LocationListener locationListener;
private LocationManager locationManager=null;
private final int MIN_TIME = 2000;
private final int MIN_DIST = 5;
private DatabaseReference databaseReference;
private EditText editTextLongitude;
private EditText editTextLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PackageManager.PERMISSION_GRANTED);
editTextLatitude = (EditText) findViewById(R.id.editText);
editTextLongitude = (EditText) findViewById(R.id.editText2);
databaseReference = FirebaseDatabase.getInstance().getReference("Location");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
try {
String databaseLatitudeString=snapshot.child("Latitude").getValue().toString().substring(1,snapshot.child("Latitude").getValue().toString().length()-1);
String databaseLongitudeString=snapshot.child("Longitude").getValue().toString().substring(1,snapshot.child("Longitude").getValue().toString().length()-1);
String[] stringLat = databaseLatitudeString.split(", ");
Arrays.sort(stringLat);
String latitude = stringLat[stringLat.length-1].split("=")[1];
String[] stringLong = databaseLongitudeString.split(", ");
Arrays.sort(stringLong);
String longitude = stringLong[stringLong.length-1].split("=")[1];
for (DataSnapshot LSnapshot: snapshot.getChildren()) {
LSnapshot.getRef().removeValue();
}
mMap.clear();
float zoomLevel = 16.0f; //This goes up to 21
LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
mMap.addMarker(new MarkerOptions().position(latLng).title(latitude + " , " + longitude));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(-34, 151);
// mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
// mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
try {
editTextLatitude.setText(Double.toString(location.getLatitude()));
editTextLongitude.setText(Double.toString(location.getLongitude()));
databaseReference.child("Latitude").push().setValue(editTextLatitude.getText().toString());
databaseReference.child("Longitude").push().setValue(editTextLongitude.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
};
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME,MIN_DIST,locationListener);
}
catch(Exception e){
e.printStackTrace();
}
}
}
even when I am implementing a basic live location app without database. Then it is also keeps stopping..
Help !!