mapView.invalidate() depuis un Thread autre que le principal
Bonjour,
je suis entrain de faire une petite application basée essentiellement sur une carte google map.
en gros, ce que je fait :
- je chope la localisation de l'utilisateur
- je fait une requête vers un site qui me renvoi un certain nombre de points autours de la position de l'utilisateur
- je crée les marqueurs de chaque point sur la carte.
mais :
en attendant de récupérer les points, j'affiche une progressDialog du style "calcul en cours..."
pour celà je crée une progressDialog, je lance mon traitement dans un nouveau Thread, et à la fin du traitement je fait un :
Code:
1 2 3
| mc.setCenter(location); // mc ==> mapController
mapView.invalidate(); //mapView ==> la mapView principale de l'appli
progressDialog.dismiss(); //je "unset" la progressDialog |
seulement j'ai cette exception srur le mapView.invalidate() :
Code:
1 2 3 4 5 6 7
| Thread [<9> Thread-16] (Suspended (exception ViewRoot$CalledFromWrongThreadException))
ViewRoot.checkThread() line: 2932
ViewRoot.invalidateChild(View, Rect) line: 642
ViewRoot.invalidateChildInParent(int[], Rect) line: 668
FrameLayout(ViewGroup).invalidateChild(View, Rect) line: 2511
MapView(View).invalidate() line: 5279
NaehActivity$1.run() line: 97 |
voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| //cette fonction est appelé quand la localisation change
public void setLocation(GeoPoint newLocation) {
this.location = newLocation;
progressDialog = ProgressDialog.show(this, getString(R.string.app_name), "Recherche des points les plus proches de votre position...");
new Thread() {
public void run() {
Points p = new Points(NaehActivity.this, location);
ArrayList<Point> points = p.getPoints();
if(points != null && points.size() > 0) {
for(Point point : points) {
addMarker(point.getLocation(), point.getNom(), point.toString());
}
}
mc.setCenter(location);
mapView.invalidate();
progressDialog.dismiss();
}
}.start();
} |
je précise que je suis débutant dans android, c'est ma 1ère application donc soyez indulgent :)
et merci par avance pour vos réponses.