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

Android Discussion :

Comment binder un service avec plusieurs activités ?


Sujet :

Android

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2009
    Messages : 23
    Points : 22
    Points
    22
    Par défaut Comment binder un service avec plusieurs activités ?
    Bonjour à tous,

    Voilà j'ai un problème que je n'arrive pas à résoudre.

    Théorie :

    J'ai crée une classe NetworkService héritant de Service qui s'occupe de toutes les actions liées au réseau. Connection / Déconnection / Requête / etc..
    J'aimerai pouvoir binder cette classe à toutes les activités de mon application, pour que chacune puisse faires des requêtes sur le serveur distant.

    Pratique :

    Première Activité

    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
            private INetwork		  iNetwork;
     
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		this.displayList();
    		this.bindService(new Intent(Welcome.this,NetworkService.class),
    				mConnection, Context.BIND_AUTO_CREATE);
    	}
     
           private ServiceConnection mConnection = new ServiceConnection() {
     
    		@Override
    		public void onServiceConnected(ComponentName arg0, IBinder service) {
    			Log.e("---->", "Entrer dans onServiceConnected");
    			iNetwork = INetwork.Stub.asInterface(service);
    		}
     
    		@Override
    		public void onServiceDisconnected(ComponentName arg0) {
    			Log.e("", "Interface Disconnected");
    		}
    	};
    Deuxième Activité

    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
    	private INetwork iNetwork;
     
    	public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            this.bindService(new Intent(Video.this,NetworkService.class),
    				mConnection, Context.BIND_AUTO_CREATE);
     
            TextView textview = new TextView(this);
            textview.setText("This is the Video tab");
            setContentView(textview);
     
        }
     
    	private ServiceConnection mConnection = new ServiceConnection() {
     
    		@Override
    		public void onServiceConnected(ComponentName arg0, IBinder service) {
    			Log.e("---->", "Entrer dans onServiceConnected");
    			iNetwork = INetwork.Stub.asInterface(service);
    			}
     
    		@Override
    		public void onServiceDisconnected(ComponentName arg0) {
    			Log.e("", "Interface Disconnected");
    		}
    	};
    Chaque fois que j'essaie de me servir dans ma deuxième activité, d'une fonction de mon service, mon application crash.


    Quelqu'un serait il pourquoi ? Peut être que je m'y prend mal même en théorie.

    Je suis ouvert à toutes vos propositions.

    Merci d'avance.

  2. #2
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    Salut.
    Citation Envoyé par Spredzy Voir le message
    mon application crash.
    Peux tu donner plus de précision?
    As tu essayé d'utiliser le debuggeur?
    Que t'affiche le logcat?

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2009
    Messages : 23
    Points : 22
    Points
    22
    Par défaut
    Voici le trace des logs :

    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
    ERROR/AndroidRuntime(631): Uncaught handler: thread main exiting due to uncaught exception
    ERROR/AndroidRuntime(631): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eip.core/com.eip.core.Video}: java.lang.NullPointerException
    ERROR/AndroidRuntime(631): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    ERROR/AndroidRuntime(631):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2335)
    ERROR/AndroidRuntime(631):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
    ERROR/AndroidRuntime(631):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
    ERROR/AndroidRuntime(631):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:648)
    ERROR/AndroidRuntime(631):     at android.widget.TabHost.setCurrentTab(TabHost.java:320)
    ERROR/AndroidRuntime(631):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
    ERROR/AndroidRuntime(631):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:379)
    ERROR/AndroidRuntime(631):     at android.view.View.performClick(View.java:2364)
    ERROR/AndroidRuntime(631):     at android.view.View.onTouchEvent(View.java:4179)
    ERROR/AndroidRuntime(631):     at android.view.View.dispatchTouchEvent(View.java:3709)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    ERROR/AndroidRuntime(631):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    ERROR/AndroidRuntime(631):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    ERROR/AndroidRuntime(631):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    ERROR/AndroidRuntime(631):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    ERROR/AndroidRuntime(631):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    ERROR/AndroidRuntime(631):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    ERROR/AndroidRuntime(631):     at android.os.Handler.dispatchMessage(Handler.java:99)
    ERROR/AndroidRuntime(631):     at android.os.Looper.loop(Looper.java:123)
    ERROR/AndroidRuntime(631):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    ERROR/AndroidRuntime(631):     at java.lang.reflect.Method.invokeNative(Native Method)
    ERROR/AndroidRuntime(631):     at java.lang.reflect.Method.invoke(Method.java:521)
    ERROR/AndroidRuntime(631):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    ERROR/AndroidRuntime(631):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    ERROR/AndroidRuntime(631):     at dalvik.system.NativeStart.main(Native Method)
    ERROR/AndroidRuntime(631): Caused by: java.lang.NullPointerException
    ERROR/AndroidRuntime(631):     at com.eip.core.Video.onCreate(Video.java:28)
    ERROR/AndroidRuntime(631):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    ERROR/AndroidRuntime(631):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    ERROR/AndroidRuntime(631):     ... 29 more

  4. #4
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    ERROR/AndroidRuntime(631): Caused by: java.lang.NullPointerException
    ERROR/AndroidRuntime(631): at com.eip.core.Video.onCreate(Video.java:28)
    A la ligne 28 de Video.java tu utilise un pointeur null. D'où ton crash.

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Août 2009
    Messages
    23
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2009
    Messages : 23
    Points : 22
    Points
    22
    Par défaut
    Mon pointeur null est mon iNetwork de ma deuxième activité.

    J'aimerais justement savoir pourquoi il est null ?
    Et donc par conséquent comment binder deux activités à un même service.

  6. #6
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968
    Par défaut
    Citation Envoyé par Spredzy Voir le message
    Mon pointeur null est mon iNetwork de ma deuxième activité.
    :quoi:
    tu as deux activity en même temps??
    et tu passe une instance de l'un vers l'autre???

Discussions similaires

  1. [MySQL] Comment affiché un résultat avec plusieurs lignes et plusieurs champs dans un tableau
    Par Yagami_Raito dans le forum PHP & Base de données
    Réponses: 18
    Dernier message: 15/05/2007, 14h19
  2. Réponses: 1
    Dernier message: 06/03/2007, 20h29
  3. VBA-E Comment réaliser une recherche avec plusieurs critères ?
    Par Larsen21 dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 14/12/2006, 13h43
  4. Comment comparer un texte avec plusieurs apostrophes
    Par electrosat03 dans le forum Access
    Réponses: 2
    Dernier message: 13/02/2006, 12h46
  5. [linux][gcc] Comment travaille t-on avec plusieurs fichiers?
    Par kaygee dans le forum Autres éditeurs
    Réponses: 2
    Dernier message: 02/04/2004, 17h48

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