IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

AlertDialog dans API Google Maps


Sujet :

API standards et tierces Android

  1. #1
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 53
    Points : 58
    Points
    58
    Par défaut AlertDialog dans API Google Maps
    Bonjour à tous.

    Je suis actuellement en train de réaliser une application de géo-localisation.
    J'ai pour cela suivi le tutoriel android suivant : http://developer.android.com/resourc...o-mapview.html

    Je n'ai aucun soucis pour ajouter mes marqueurs sur la carte qui permettent de savoir ou je suis passé mais le soucis arrive lorsque je souhaite clicker sur un marqueurs android qui doit m'afficher une boite de dialogue.
    L'erreur se produit à l'appel de la méthode onTap().

    J'obtiens l'erreur suivante :

    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
     
    11-28 19:05:17.548: E/AndroidRuntime(3412): FATAL EXCEPTION: main
    11-28 19:05:17.548: E/AndroidRuntime(3412): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewRoot.setView(ViewRoot.java:513)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.app.Dialog.show(Dialog.java:241)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.mowegan.geoloc.TrackItemizedOverlay.onTap(TrackItemizedOverlay.java:50)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.google.android.maps.MapView.onTouchEvent(MapView.java:647)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.View.dispatchTouchEvent(View.java:3765)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.app.Activity.dispatchTouchEvent(Activity.java:2093)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.os.Looper.loop(Looper.java:144)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at android.app.ActivityThread.main(ActivityThread.java:4937)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at java.lang.reflect.Method.invokeNative(Native Method)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at java.lang.reflect.Method.invoke(Method.java:521)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    11-28 19:05:17.548: E/AndroidRuntime(3412): 	at dalvik.system.NativeStart.main(Native Method)
    Je vous joins mes 2 classes qui sont calqué sur le tutoriel android

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
     
    package com.mowegan.geoloc;
     
    import java.util.List;
     
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.Toast;
     
    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapActivity;
    import com.google.android.maps.MapController;
    import com.google.android.maps.MapView;
    import com.google.android.maps.MyLocationOverlay;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.OverlayItem;
     
     
    public class GeoSportActivity extends MapActivity implements LocationListener{
     
    	private MapView mapView;
    	private LocationManager lm;
    	private MapController mc;
    	private MyLocationOverlay myPosition;
     
    	TrackItemizedOverlay itemizedOverlay;	// Overlay with the track of the user
     
    	private double lat;						// Latitude
    	private double lng;						// Longitude
     
    	private TextView txtLat;				// Latitude text field
    	private TextView txtLng;				// Longitude text field
     
    	/** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
     
        	// Set the content of the activity
        	super.onCreate(savedInstanceState);
        	setContentView(R.layout.main);
     
        	// Create field text for latitude and longitude
        	txtLat = (TextView)findViewById(R.id.txtLatitude);
        	txtLng = (TextView)findViewById(R.id.txtLongitude);
     
        	// Get the map view
        	mapView = (MapView)this.findViewById(R.id.mapView);
        	mapView.setBuiltInZoomControls(true);
        	mapView.setSatellite(true);
     
     
        	// Build the location manager
        	lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, this);
        	lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 10, this);
     
     
        	// Build of the map controller
        	mc = mapView.getController();
        	mc.setZoom(12);
     
        	// Build the location overlay
        	myPosition = new MyLocationOverlay(this.getApplicationContext(), mapView);
        	mapView.getOverlays().add(myPosition);
        	myPosition.enableMyLocation();
     
        	// Add a compass to the map
        	myPosition.enableCompass();
     
     
        	// Build the itemized overlay
        	// Create the drawable to insert on the overlay
        	Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
        	this.itemizedOverlay = new TrackItemizedOverlay(drawable, getBaseContext());
     
        }
     
    	@Override
    	protected boolean isRouteDisplayed() {
    		return false;
    	}
     
    	public void onLocationChanged(Location location) {
     
    		// Get new position of the mobile phone
    		this.lat = location.getLatitude();
    		this.lng = location.getLongitude();
     
    		// Set the value of the text field for lat and lng position
    		txtLat.setText(Double.toString(this.lat));
    		txtLng.setText(Double.toString(this.lng));
    		this.addPoint(this.lat, this.lng);
     
    		// Create a new point corresponding to the new location
    		//GeoPoint point = new GeoPoint((int)(this.lat * 1E6), (int)(this.lng * 1E6));
    		//this.mc.animateTo(point);
    		//this.mc.setCenter(point);
     
     
    		Toast.makeText(getBaseContext(),"Latitude = " + lat + " Longitude = " + lng, Toast.LENGTH_SHORT).show();
     
    	}
     
    	public void onProviderDisabled(String provider) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void onProviderEnabled(String provider) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void onStatusChanged(String provider, int status, Bundle extras) {
    		// TODO Auto-generated method stub
     
    	}
     
    	public void addPoint(double lat, double lng){
     
    		// Get all the overlays of the map view
        	List<Overlay> mapOverlays = mapView.getOverlays();
     
        	// Create a new point
        	GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
     
        	// Create a new new item
        	OverlayItem overlayItem = new OverlayItem(point, "Test", "Test des marqueurs");
     
        	// Add item to the overlay
        	this.itemizedOverlay.addTrackItem(overlayItem);
     
        	// Add the overlay on the other overlay
        	mapOverlays.add(itemizedOverlay);
    	}
    }
    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
    60
     
    package com.mowegan.geoloc;
     
    import java.util.ArrayList;
     
    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
     
     
    import com.google.android.maps.ItemizedOverlay;
    import com.google.android.maps.OverlayItem;
     
    public class TrackItemizedOverlay extends ItemizedOverlay<OverlayItem> {
     
    	private ArrayList<OverlayItem> trackItems;	// List of items
    	private Context context;					// Reference to the context activity
     
     
    	// Constructor with drawable
    	public TrackItemizedOverlay(Drawable defaultMarker) {
    		super(boundCenterBottom(defaultMarker));
    		this.trackItems = new ArrayList<OverlayItem>();
    	}
     
    	// Constructor with drawable and activity sequence
    	public TrackItemizedOverlay(Drawable defaultMarker, Context context){
    		super(boundCenterBottom(defaultMarker));
    		this.trackItems = new ArrayList<OverlayItem>();
    		this.context = context;
    	}
     
    	@Override
    	protected OverlayItem createItem(int i) {
    		return trackItems.get(i);
    	}
     
    	@Override
    	public int size() {
    		return trackItems.size();
    	}
     
    	@Override
    	protected boolean onTap(int index) {
     
    		OverlayItem item = this.trackItems.get(index);
     
    		AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
    		dialog.setTitle(item.getTitle());
    		dialog.setMessage(item.getSnippet());
    		dialog.show();
     
    		return true;
    	}
     
    	public void addTrackItem(OverlayItem item){
    		trackItems.add(item);
    		populate();
    	}
    }
    Cela fait maintenant un bon moment que je cherches sur les différents forum mais je ne trouve toujours pas la solution.

    Je m'adresse donc à vous.

    Je vous remercie d'avance pour votre aide précieuse.

  2. #2
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    Tu as un problème sur ton context, essayes plutôt d'afficher ton dialogue à partir de ton Activity au lieu de vouloir l'afficher à partir de ton Overlay.

    Pour cela tu peux passer par un handler depuis ton overlay qui appellera ton Activity .

    Après si tu tiens absolument l'afficher depuis ton Overlay alors cela ne sers à rien de passer par un getBaseContext() , utilise plutôt getApplicationContext();

    Utilises le moins possible le getBaseContext(); c'est souvent source d'erreur ...
    http://stackoverflow.com/questions/1...-get-a-context
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  3. #3
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 53
    Points : 58
    Points
    58
    Par défaut
    J'ai bien essayé de passer comme paramètre getApplicationContext() au lieu de getBaseContext() mais l'application plante toujours lorsque je clique sur un de mes marqueurs android.

    Je vais essayer en passant par un handler comme tu me l'as conseillé.

    Merci bien

  4. #4
    Membre actif Avatar de chpil
    Homme Profil pro
    Inscrit en
    Octobre 2011
    Messages
    143
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations forums :
    Inscription : Octobre 2011
    Messages : 143
    Points : 212
    Points
    212
    Par défaut
    Il faudrait plutôt passer l'Activity (une Activity est elle même un Context)

  5. #5
    Membre du Club
    Profil pro
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 53
    Points : 58
    Points
    58
    Par défaut
    A merci bien chpil, ça marche à merveilles tout ceci.

    Je vais partir sur cette solution.

    Merci à vous deux.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. De l'ajax dans du javascript (API Google Map)
    Par squal31 dans le forum jQuery
    Réponses: 1
    Dernier message: 26/08/2014, 22h33
  2. introduire une API google map dans visual studio
    Par visualstudio dans le forum C#
    Réponses: 1
    Dernier message: 20/05/2011, 09h08
  3. [Google Maps] Informations dans les bulles
    Par artichaudd dans le forum APIs Google
    Réponses: 0
    Dernier message: 15/04/2010, 18h42
  4. [Google Maps] IE6 refuse d'ouvrir ma page
    Par Shyboy dans le forum APIs Google
    Réponses: 5
    Dernier message: 03/10/2006, 08h47
  5. [Google Maps] Intégrer de nouvelles icônes
    Par Shyboy dans le forum APIs Google
    Réponses: 7
    Dernier message: 01/10/2006, 00h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo