Jump to content

[RESOLU] Lancer une nouvelle activité


Femto

Recommended Posts

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 by Femto
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

    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 by acesyde
Link to comment
Share on other sites

    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 by Femto
Link to comment
Share on other sites

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 by Femto
Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

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 by fusox
Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...