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:
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:
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:
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. :)