Android Google Maps V2 position actuelle latitude longitude NullPointerException

Il y a beaucoup de question similaire, mais je ne trouve pas de solution pour mon problème.
Le setUpMap méthode est la suivante:

private void setUpMap() {
BitmapDescriptor iconm = BitmapDescriptorFactory.fromResource(R.drawable.m);
BitmapDescriptor iconc = BitmapDescriptorFactory.fromResource(R.drawable.c);
//Enable MyLocation Layer of Google Map
mMap.setMyLocationEnabled(true);
//Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
//Get the name of the best provider
String provider;
provider = locationManager.getBestProvider(criteria,true);
//Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//getting GPS status
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
boolean isNWEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isNWEnabled)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage(getString(R.string.askposition) );
dialog.setPositiveButton(getString(R.string.settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
//TODO Auto-generated method stub
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
//TODO Auto-generated method stub
}
});
dialog.show();
//no network provider is enabled
Log.e("Current Location", "Current Lat Lng is Null");
}
else
{
//if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled){
if (myLocation == null) {
if (locationManager != null) {
myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();
//String lati = String.valueOf(latitude);
//String longi = String.valueOf(longitude);
//Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
//set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//Show the current location in Google Map
//mMap.moveCamera(newLatLng(latlng));
//Zoom in the Google Map
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
}
}
else {
//First get location from Network Provider
if (isNWEnabled) {
if (myLocation == null) {
if (locationManager != null) {
myLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
//double latitude = myLocation.getLatitude();
//double longitude = myLocation.getLongitude();
//Create a LatLng object for the current location
//LatLng latlng = new LatLng(latitude, longitude);
//set map type
//mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
//Zoom in the Google Map
//CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(12).build();
//mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(12));
//Show the current location in Google Map
//mMap.moveCamera(newLatLng(latlng));
}
}
}}
}
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latlng = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(latlng).zoom(11).build();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Il fonctionne très bien lorsque je la première Exécution de l'application sur Android Studio pour mon appareil, fonctionne aussi très bien si j'ai fermer et redémarrer l'application.
Mais quand je redémarre mon smartphone, il me donne NullPointerException @ double latitude = myLocation.getLatitude();

et à partir de ce moment, il crash toujours.
Comment pourrais-je résoudre ce problème?
Le code commenté les lignes sont à propos de quelques tentatives. Même problème à tous.
Toute aide serait grandement appréciée.
Merci à l'avance.

developer.android.com/reference/android/location/.... Citant docs Si le fournisseur est actuellement désactivé, la valeur null est retournée. Vérifiez également ce gabesechansoftware.com/location-tracking/#comments. Mise à jour : developer.android.com/training/location/...
vous pouvez éviter le crash de l'utilisation de try catch..mettez votre code à l'intérieur de try catch
Juste une suggestion. Si vous êtes en développement dans Android Studio, vous pouvez utiliser: github.com/mcharmas/Android-ReactiveLocation pour obtenir les dernières fusibles emplacement
lorsque u redémarrer le téléphone, le GPS est activé?
Je vais regarder merci. Kat-chapeau, je vais essayer, mais je sais pas pourquoi. cYrixmorten je vous remercie pour votre suggestion, j'ai essayer plus tard, en espérant résoudre avec le code réel. Manny264 oui, il est. Le réseau et le GPS à la fois

OriginalL'auteur Nonjoe | 2015-03-05