Dans mon app j'ai une activité qui permet de laisser un commentaire avec nom et date. Tout cela fonctionne parfaitement. J'ai décidé de rajouter la ville au commentaire et ai donc ajouté des fonctions simples pour faire cela.
Le soucis est dans la méthode obtenirPosition() voir code ci-dessous :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 public class CommentsActivity extends Activity { private SingleComment comment_to_record = null; private LocationManager lManager; private Location location; private String choix_source = null; private String localisation = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.lay_submit_comments); comment_to_record = new SingleComment(); lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); }
Avec le debugger j'ai repèré que location était null même après l'execution de getLastKnownLocation(choix_source) alors que choix_source est bien défini.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 private String doLocation() { setProgressBarIndeterminateVisibility(true); // on démarre le cercle de chargement location = obtenirPosition(); // Le geocoder permet de récupérer ou chercher des adresses // gràce à un mot clé ou une position Geocoder geo = new Geocoder(getApplicationContext()); try { // Ici on récupère la premiere adresse trouvée gràce à la position // que l'on a récupérée List<Address> adresses = geo.getFromLocation( location.getLatitude(), location.getLongitude(), 1); if (adresses != null && adresses.size() == 1) { Address adresse = adresses.get(0); // Si le geocoder a trouver une adresse, alors on l'affiche localisation = adresse.getLocality(); // Si l'adresse est nulle on affiche la parenthèse if (localisation == null) { localisation = " ( . . . ) "; } } else { // sinon on affiche un message d'erreur Toast.makeText(getApplicationContext(), String.format("L'adresse n'a pu être déterminée"), Toast.LENGTH_SHORT).show(); localisation = "( . . . )"; } } catch (IOException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), String.format("L'adresse n'a pu être déterminée"), Toast.LENGTH_SHORT).show(); localisation = "( . . . )"; } setProgressBarIndeterminateVisibility(false); return localisation; } private String choisirSource() { choix_source = LocationManager.NETWORK_PROVIDER; // On demande au service la liste des sources disponibles. List<String> providers = lManager.getProviders(true); Iterator<String> i = providers.iterator(); while (i.hasNext()) { String provider = (String) i.next(); if (provider.equals(LocationManager.GPS_PROVIDER)) { choix_source = LocationManager.GPS_PROVIDER; } } return choix_source; } private Location obtenirPosition() { choix_source = choisirSource(); location = lManager.getLastKnownLocation(choix_source);////////////// ICI!!! return location; } }
Toutes idées bienvenues.
Merci.
Partager