Warum kommt manchmal null raus?

Hallo. Ich mache gerade eine App, die den Standort des Users finden soll. Manchmal klappt es und manchmal kommt null raus. Hier mal der Code:

lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationTracker);
lManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationTracker, null);
userLocation = locationTracker.lastUserLocation;

Location gpsLocation = null;
Location networkLocation = null;
Location passiveLocation = null;

try {
    gpsLocation = lManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (SecurityException e) {
    Toast.makeText(getActivity(), "Error: SecurityException. (" + e + ")", Toast.LENGTH_SHORT).show();
}

try {
    networkLocation = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
} catch (SecurityException e) {
    Toast.makeText(getActivity(), "Error: SecurityException. (" + e + ")", Toast.LENGTH_SHORT).show();
}

try {
    passiveLocation = lManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
} catch (SecurityException e) {
    Toast.makeText(getActivity(), "Error: SecurityException. (" + e + ")", Toast.LENGTH_SHORT).show();
}

if (gpsLocation != null) {
    userLocation = gpsLocation;
} else if (networkLocation != null) {
    userLocation = networkLocation;
} else if (passiveLocation != null) {
    userLocation = passiveLocation;
} else if(locationTracker.lastUserLocation != null) {
    Toast.makeText(getActivity(), "Using last known location", Toast.LENGTH_SHORT).show();
    userLocation = locationTracker.lastUserLocation;
} else {
    Toast.makeText(getActivity(), "Error: Unable to get your location. Try Again.", Toast.LENGTH_SHORT).show();
    userLocation = null;
}

Das ist der Code, der onClick auf einen Button ausgeführt wird. in meiner onViewCreate Methode hab ich außerdem noch:

lManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if(getActivity().checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, Process.myPid(), Process.myUid()) == PackageManager.PERMISSION_GRANTED) {
    lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationTracker);
}

Was ich nur nicht verstehe, wieso genau kommt da denn manchmal null raus? Ich sende doch eine request damit der Standort geupdatet wird. Jemand vielleicht eine Idee?

Danke im Voraus und lg.

P.S.:

Getestet auf ZTE Blade V10 (Android 9, API Level 29). Die ganze App ist auf Level 21.

PC, Computer, Handy, Internet, Software, App, Technik, programmieren, Java, GPS, Android, GPS Ortung, Softwareentwicklung, Technologie

Meistgelesene Fragen zum Thema Handy