Femto Posted October 19, 2010 Share Posted October 19, 2010 (edited) Bonjour, Généralement lorsque l'on veut lancer une nouvelle activité dans une classe qui étend (extends) "activity", il suffit de faire startactivity(un_Intent). Le problème c'est que je voudrais faire la même chose depuis une classe qui étend "ItemizedOverlay". Je l'utilise pour afficher des marqueurs sur une carte google maps. Dedans j'ai une méthode "onTap(int i)" pour que quand j'appuie sur le marqueur positionnée sur la carte j'appelle le browser avec un lien donnée et j'ai eu cette idée: protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); Activity my_activity = new Activity(); my_activity.startActivity(myintent); return (true); } le problème est que mon programme plante lorsque j'appuie sur le marqueur (fermeture forcée). Quelqu'un aurait il une idée ? Merci d'avance pour votre aide =) Voici aussi le code complet de ma classe pour ceux qui ne verraient pas clair: public class MapOverlay extends ItemizedOverlay{ private List items = new ArrayList(); private Drawable marker = null; private Context context = null; private GeoPoint mypoint; //constructeur de ma classe qui lorsque appelé me positionne les marqueurs sur ma carte public MapOverlay(Drawable marker, Context context) { super(marker); this.marker = marker; this.context = context; double latitude =46.2489*1E6; double longitude=6.1217*1E6; mypoint = new GeoPoint((int)latitude,(int)longitude); items.add(new OverlayItem(mypoint,"Maison", "Ma maison")); populate(); } protected OverlayItem createItem(int i) { return (items.get(i)); } public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow); boundCenterBottom(marker); } protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); Activity my_activity = new Activity(); my_activity.startActivity(myintent); return (true); } public int size() { return (items.size()); } } Edited October 21, 2010 by Femto Quote Link to comment Share on other sites More sharing options...
zippeurfou Posted October 19, 2010 Share Posted October 19, 2010 post le logcat cela pourrait aider :) Quote Link to comment Share on other sites More sharing options...
Femto Posted October 19, 2010 Author Share Posted October 19, 2010 post le logcat cela pourrait aider :) Voilà ce que j'ai lorsque je touche mon marqueur: 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): FATAL EXCEPTION: main 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): java.lang.NullPointerException 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.app.Activity.startActivityForResult(Activity.java:2858) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.app.Activity.startActivity(Activity.java:2964) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.cardoso.localisationtpg.MapOverlay.onTap(MapOverlay.java:50) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.google.android.maps.ItemizedOverlay.onTap(ItemizedOverlay.java:453) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.google.android.maps.OverlayBundle.onTap(OverlayBundle.java:83) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.google.android.maps.MapView$1.onSingleTapUp(MapView.java:347) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.google.android.maps.GestureDetector.onTouchEvent(GestureDetector.java:533) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.google.android.maps.MapView.onTouchEvent(MapView.java:647) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.View.dispatchTouchEvent(View.java:3765) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:905) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:944) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.app.Activity.dispatchTouchEvent(Activity.java:2093) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.os.Handler.dispatchMessage(Handler.java:99) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.os.Looper.loop(Looper.java:144) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at android.app.ActivityThread.main(ActivityThread.java:4937) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at java.lang.reflect.Method.invokeNative(Native Method) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at java.lang.reflect.Method.invoke(Method.java:521) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-19 20:32:41.167: ERROR/AndroidRuntime(1239): at dalvik.system.NativeStart.main(Native Method) Quote Link to comment Share on other sites More sharing options...
acesyde Posted October 19, 2010 Share Posted October 19, 2010 (edited) protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); Activity my_activity = new Activity(); my_activity.startActivity(myintent); return (true); } protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); startActivity(myintent); return (true); } Plutôt ça non ? Edited October 19, 2010 by acesyde Quote Link to comment Share on other sites More sharing options...
Femto Posted October 19, 2010 Author Share Posted October 19, 2010 (edited) protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); startActivity(myintent); return (true); } Plutôt ça non ? relis bien ce que j'ai écrit. Ma classe mapOverlay n'étend pas "activity" mais "ItemizedOverlay", donc je ne peux pas faire startactivity. startActivity est une méthode propre aux classes qui étendent "activity". :) édit: du genre "public class main extends Activity" Edited October 19, 2010 by Femto Quote Link to comment Share on other sites More sharing options...
Fluckysan Posted October 20, 2010 Share Posted October 20, 2010 Tu peux lancer ta nouvelle Activity via le Context (il faut que tu l'enregistre au new par exemple) : http://developer.android.com/reference/android/content/Context.html#startActivity(android.content.Intent) Quote Link to comment Share on other sites More sharing options...
Femto Posted October 20, 2010 Author Share Posted October 20, 2010 (edited) Alors j'ai essayé ça: protected boolean onTap(int i) { Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("www.frandroid.com")); myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); this.context.getApplicationContext().startActivity(myintent); return (true); } mais toujours un crash :/ , j'ai aussi test avec d'autres flags comme le "FLAG_GRANT_WRITE_URI_PERMISSION" merci de m'aider ça m'apprendra à mieux lire la doc :), bon je continue de creuser édit: j'ai aussi test sans le "this" Edited October 20, 2010 by Femto Quote Link to comment Share on other sites More sharing options...
Femto Posted October 21, 2010 Author Share Posted October 21, 2010 Trouvé ! Dans ma classe MapOverlay j'ai ajouter une méthode que j'appelle dans ma Mapactivity: public void Getthecontext(Context context) { basecontext=context; } puis dans le onTap : protected boolean onTap(int i) { //Toast.makeText(context, items.get(i).getSnippet(), Toast.LENGTH_SHORT).show(); Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("l'url")); myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); basecontext.startActivity(myintent); return (true); } Et donc dans mon mapactivity j'ai juste à lui passer le "this.getBaseContext()" dans ma méthode crée =) Quote Link to comment Share on other sites More sharing options...
nini29 Posted December 8, 2010 Share Posted December 8, 2010 Bonjour, J'ai la même erreur mais ta solution ne fonctionne pas ? Du coup je me demandai tu avais définie chaque activity dans le manifeste? merci Quote Link to comment Share on other sites More sharing options...
fusox Posted January 20, 2012 Share Posted January 20, 2012 (edited) Trouvé ! Dans ma classe MapOverlay j'ai ajouter une méthode que j'appelle dans ma Mapactivity: public void Getthecontext(Context context) { basecontext=context; } puis dans le onTap : protected boolean onTap(int i) { //Toast.makeText(context, items.get(i).getSnippet(), Toast.LENGTH_SHORT).show(); Intent myintent = new Intent(Intent.ACTION_VIEW, Uri.parse("l'url")); myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); basecontext.startActivity(myintent); return (true); } Et donc dans mon mapactivity j'ai juste à lui passer le "this.getBaseContext()" dans ma méthode crée =) j' aimerais lancer une activité en faisant la même chose (j'ai des marqueur sur ma carte avec de la géolocalisation pour l'instant...) mais avec : Intent intent = new Intent(Calque.this,/*letitredumarqueur*/.class); startActivity(intent); mais je débute tout juste le développement sous android peux tu m'aider Edited January 20, 2012 by fusox Quote Link to comment Share on other sites More sharing options...
fusox Posted January 31, 2012 Share Posted January 31, 2012 Problème résolu Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.