flyer74 Posted January 10, 2012 Share Posted January 10, 2012 Bonjours, j'ai un petit problème avec l'utilisation du GPS, j'aurais voulus renvoyer l'altitude, le cap et la vitesse à l'utilisateur, en utilisant respectivement les méthodes : hasSpeed() hasBearing() hasAltitude() Donc je fais mon code: package com.gps.android; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView; public class ToolBoxActivity extends Activity implements LocationListener { private LocationManager lManager; private Location location; private String choix_source = ""; TextView TxtVitesse; TextView TxtCap; TextView TxtAlti; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tool); TxtVitesse = (TextView) findViewById(R.id.TxtVitesse); TxtCap = (TextView) findViewById(R.id.TxtCap); TxtAlti = (TextView) findViewById(R.id.TxtAlti); lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //Initialisation de l'écran reinitialisationEcran(); choisirSource(); obtenirPosition(); afficherPosition(); } private void reinitialisationEcran() { ((TextView)findViewById(R.id.TxtVitesse)).setText("0.0"); ((TextView)findViewById(R.id.TxtCap)).setText("0.0"); ((TextView)findViewById(R.id.TxtAlti)).setText("0.0"); } private void choisirSource() { reinitialisationEcran(); //On demande au service la liste des sources disponibles. List providers = lManager.getProviders(true); final String[] sources = new String[providers.size()]; int i =0; //on stock le nom de ces source dans un tableau de string for(String provider : providers) sources[i++] = provider; //On affiche la liste des sources dans une fenêtre de dialog new AlertDialog.Builder(ToolBoxActivity.this) .setItems(sources, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //on stock le choix de la source choisi choix_source = sources[which]; //on ajoute dans la barre de titre de l'application le nom de la source utilisé setTitle(String.format("%s - %s", getString(R.string.app_name), choix_source)); } }) .create().show(); } private void obtenirPosition() { //On demande au service de localisation de nous notifier tout changement de position //sur la source (le provider) choisie, seconde (60millisecondes). //Le paramètre this spécifie que notre classe implémente LocationListener et recevra //les notifications. lManager.requestLocationUpdates(choix_source, 60, 0, this); } private void afficherPosition() { //On affiche les informations de la position a l'écran ((TextView)findViewById(R.id.TxtVitesse)).setText(String.valueOf(location.hasSpeed())); ((TextView)findViewById(R.id.TxtCap)).setText(String.valueOf(location.hasBearing())); ((TextView)findViewById(R.id.TxtAlti)).setText(String.valueOf(location.hasAltitude())); } @Override public void onLocationChanged(Location arg0) { } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } Mais voila l'erreur retourner via le logCat : 01-10 17:17:24.299: E/AndroidRuntime(454): FATAL EXCEPTION: main 01-10 17:17:24.299: E/AndroidRuntime(454): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aviation.copilot.android/com.aviation.copilot.android.ToolBoxActivity}: java.lang.IllegalArgumentException: provider= 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.os.Handler.dispatchMessage(Handler.java:99) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.os.Looper.loop(Looper.java:126) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread.main(ActivityThread.java:3997) 01-10 17:17:24.299: E/AndroidRuntime(454): at java.lang.reflect.Method.invokeNative(Native Method) 01-10 17:17:24.299: E/AndroidRuntime(454): at java.lang.reflect.Method.invoke(Method.java:491) 01-10 17:17:24.299: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 01-10 17:17:24.299: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 01-10 17:17:24.299: E/AndroidRuntime(454): at dalvik.system.NativeStart.main(Native Method) 01-10 17:17:24.299: E/AndroidRuntime(454): Caused by: java.lang.IllegalArgumentException: provider= 01-10 17:17:24.299: E/AndroidRuntime(454): at android.os.Parcel.readException(Parcel.java:1326) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.os.Parcel.readException(Parcel.java:1276) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446) 01-10 17:17:24.299: E/AndroidRuntime(454): at com.aviation.copilot.android.ToolBoxActivity.obtenirPosition(ToolBoxActivity.java:94) 01-10 17:17:24.299: E/AndroidRuntime(454): at com.aviation.copilot.android.ToolBoxActivity.onCreate(ToolBoxActivity.java:44) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 01-10 17:17:24.299: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 01-10 17:17:24.299: E/AndroidRuntime(454): ... 11 more Et apparament il y aurais une erreur avec le provider mais je ne vois pas du tout .. Y'a t-il quelque chose que je n'ais pas bien fait ? Merci Flyer74 Link to comment Share on other sites More sharing options...
flyer74 Posted January 14, 2012 Author Share Posted January 14, 2012 Personne ? Link to comment Share on other sites More sharing options...
chpil Posted January 16, 2012 Share Posted January 16, 2012 Le problème vient de ta façon de ne pas gérer l'asynchronisme présent un peu partout Android, et notamment pour le GPS. Tu ne peux pas appeler, comme tu le fais dans ton code, tes méthodes les unes après les autres, sans attendre le résultat (asynchrone) de la précédente. Tu te retrouves à utiliser des variables qui ne sont pas encore affectées, d'où ton plantage Pour résoudre ton pb, il faut que tu supprimes, dans le onCreate, les appels à obtenirPosition et afficherPosition; que tu rajoutes l'appel à obtenirPosition dans le onClick du Listener sur le dialog de choix des sources de localisation; et que tu fasses l'appel à afficherPosition dans le onLocationChanged (en te basant sur l'argument arg0 pour récupérer la position courante) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.