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

Composants graphiques Android Discussion :

Thread et Dialog


Sujet :

Composants graphiques Android

  1. #1
    Membre éprouvé Avatar de LeXo
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    1 147
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 1 147
    Par défaut Thread et Dialog
    Bonjour

    Bonjour

    Dans un Thread, je fais un traitement
    et je veux afficher un message d'avertissement

    et j'ai un message d'erreur sur le "show"

    Erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874): FATAL EXCEPTION: Thread-8
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.os.Handler.<init>(Handler.java:121)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.app.Dialog.<init>(Dialog.java:101)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.app.AlertDialog.<init>(AlertDialog.java:63)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.app.AlertDialog.<init>(AlertDialog.java:59)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.app.AlertDialog$Builder.create(AlertDialog.java:786)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at android.app.AlertDialog$Builder.show(AlertDialog.java:801)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at org.karkam.huggy.alert.LocationAlertManager.alert(LocationAlertManager.java:37)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at org.karkam.huggy.alert.LocationAlertManager.manage(LocationAlertManager.java:26)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at org.karkam.huggy.service.AlertServiceTask.calculDistance(AlertServiceTask.java:72)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at org.karkam.huggy.service.AlertServiceTask.run(AlertServiceTask.java:40)
    05-28 14:45:21.900: ERROR/AndroidRuntime(4874):     at java.lang.Thread.run(Thread.java:1102)

    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
     
     
    public void run() {
    		// TODO Auto-generated method stub
    		try {
    			while (true) {
    				synchronized (this) {
     
     
     
    					this.wait( 3000 );
    AlertDialog.Builder alertbox = new AlertDialog.Builder(c);
     
            // set the message to display
            alertbox.setMessage("This is the alertbox!");
     
            // add a neutral button to the alert box and assign a click listener
            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
     
                // click listener on the alert box
                public void onClick(DialogInterface dialog, int arg1) {
                    // the button was clicked
                	dialog.dismiss();
                }
            });
     
            // show it
            alertbox.show();;
    				}
    			}
    		} catch (InterruptedException  ie) {
    			Log.d(getClass().getSimpleName(), "AlertServiceTask interrupted.");
    		}
    	}

  2. #2
    Membre averti
    Homme Profil pro
    Inscrit en
    Avril 2011
    Messages
    52
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2011
    Messages : 52
    Par défaut
    Je ne sais pas trop comment tu t'y prends
    poste pas très claire

    Mais je voulais juste dire que seul l'UI Thread a le droit de modifier la vue.
    sinon faudra mettre en place un mecanisme d'handler ou RunOnUIThread ou encore utiliser un AsynTask

  3. #3
    Membre chevronné
    Homme Profil pro
    IUT Informatique
    Inscrit en
    Mars 2011
    Messages
    412
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : IUT Informatique

    Informations forums :
    Inscription : Mars 2011
    Messages : 412
    Par défaut
    ton alertbox.show est bien dans le fichier LocationAlertManager.java à la ligne 37??

  4. #4
    Membre éprouvé Avatar de LeXo
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    1 147
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 1 147
    Par défaut
    Dans une activité, j'ai un bouton qui démarre un local service

    ce service démarre un thread et dans ce thread, après un traitement je souhaites afficher un message d'avertissement sous la forme d'un alertDialog.

    le fait est que sur la méhode show du dialogue j'ai cette erreur,
    compte tenu que la modificaiton des UI ne peut se faire que dans l'UI Thread.
    en outre,
    la méthode runonUIThread est une méthode d'Activity

    je continues donc à chercher

  5. #5
    Membre expérimenté
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2005
    Messages
    103
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2005
    Messages : 103
    Par défaut
    Pour régler le problème vous pouvez utiliser "Looper.prepare()", le code de l'alert puis "Looper.loop();"

  6. #6
    Membre éprouvé Avatar de LeXo
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    1 147
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 1 147
    Par défaut
    j'ai réussi avec un handler à afficher un message

    alors j'arrive à afficher un Toast mais je n'arrive pas à afficher un dialog du genre ProgressDialog ou AlertDialog

    j'ai cette erreur, qui est normalement une erreur de context, or dans le service, je récupère le context en faisant un getApplicationContext


    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
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591): FATAL EXCEPTION: main
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.view.ViewRoot.setView(ViewRoot.java:513)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.app.Dialog.show(Dialog.java:241)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at org.karkam.huggy.alert.LocationAlertManager$1.run(LocationAlertManager.java:49)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.os.Handler.handleCallback(Handler.java:587)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.os.Handler.dispatchMessage(Handler.java:92)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.os.Looper.loop(Looper.java:143)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at android.app.ActivityThread.main(ActivityThread.java:4914)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    05-29 17:13:45.408: ERROR/AndroidRuntime(1591):     at dalvik.system.NativeStart.main(Native Method)

  7. #7
    Membre chevronné
    Homme Profil pro
    IUT Informatique
    Inscrit en
    Mars 2011
    Messages
    412
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : IUT Informatique

    Informations forums :
    Inscription : Mars 2011
    Messages : 412
    Par défaut
    J'avais eu ce problème.

    T'as deux solutions. Si t'es dans ton activity dans laquelle tu veux affiché, tu fais un this à la place du getApplicationContext, et si t'es dans un autre thread, tu passe en paramètre à ce thread celui de ton activity qui est affiché en faisant un this...Je sais pas si j'ai bien été clair ^^.

Discussions similaires

  1. Ouverture d'un dialog modal dans un thread
    Par MoonLightpf dans le forum wxPython
    Réponses: 7
    Dernier message: 05/02/2010, 13h05
  2. Dialog sur autre Thread
    Par deejay2221 dans le forum C#
    Réponses: 1
    Dernier message: 30/03/2009, 22h35
  3. [MFC] Thread de communication et Dialog Data Exchange
    Par Philippe320 dans le forum MFC
    Réponses: 4
    Dernier message: 20/05/2006, 17h07
  4. [MFC] dialog & thread & progressbar
    Par Tigris94 dans le forum MFC
    Réponses: 4
    Dernier message: 05/04/2005, 15h51
  5. message entre dialog box et thread
    Par scorbo dans le forum MFC
    Réponses: 7
    Dernier message: 19/06/2004, 11h01

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