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 :

Probleme progress dialog


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2011
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2011
    Messages : 58
    Par défaut Probleme Progress Dialog
    Bonjour,

    Je sais pas pourquoi, ce code ne marche pas

    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
    38
     
    		public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
    				long arg3) {
     
    			 myProgressDialog = ProgressDialog.show(ProgressBarAppli.this, 
      	                "Please wait...", "Doing Extreme Calculations...", true);
     
    	        new Thread() {
                    public void run() {
                            try{
                            	ville=(TextView)findViewById(R.id.ville);
                        		population=(TextView)findViewById(R.id.population);
                        		longtitude=(TextView)findViewById(R.id.longtitude);
                        		latitude=(TextView)findViewById(R.id.latitude);
     
                        	 	Spinner AccCurr = (Spinner) findViewById(R.id.spinner); 
                    			sAccCurr = AccCurr.getSelectedItem().toString();
     
                    			chaine=getHttpResponse(sAccCurr);
                    			String[] result=splitResult(chaine);
                    			ville.setText(sAccCurr);
                    			population.setText(result[1]);
                    			longtitude.setText(result[2]);
                    			latitude.setText(result[3]);
                            sleep(5000);
     
                            } catch (Exception e) { }
                            // Dismiss the Dialog 
                            myProgressDialog.dismiss();
                    }
            }.start();
     
     
    			Log.i("end", "end");
     
     
     
    		}
    Merci.

  2. #2
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,
    Je sais pas pourquoi, ce code ne marche pas
    Pourrais tu préciser en quoi il ne marche pas ?

    Si tu as une erreur pense à nous poster également l'erreur dans le logcat .

    N'hésite pas non plus à utiliser le débeggeur pour mieux comprendre d'où provient le non fonctionnement de ton application .

    Merci .

  3. #3
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2011
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2011
    Messages : 58
    Par défaut
    Le probleme que j'ai aucun erreur ; mais le code entre try et catch n'est jamais executé
    la partie avant le thread est executé et aussi la partie qui est apres le thread

  4. #4
    Membre chevronné
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    322
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 322
    Par défaut
    Mets un @Override avant ta méthode run().

  5. #5
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2011
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2011
    Messages : 58
    Par défaut
    ça marche toujours pas

  6. #6
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour ,

    Essaye dèjà comme cela

    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
    Thread thread = new Thread()
    {
        @Override
        public void run() {
           try{
                            	ville=(TextView)findViewById(R.id.ville);
                        		population=(TextView)findViewById(R.id.population);
                        		longtitude=(TextView)findViewById(R.id.longtitude);
                        		latitude=(TextView)findViewById(R.id.latitude);
     
                        	 	Spinner AccCurr = (Spinner) findViewById(R.id.spinner); 
                    			sAccCurr = AccCurr.getSelectedItem().toString();
     
                    			chaine=getHttpResponse(sAccCurr);
                    			String[] result=splitResult(chaine);
                    			ville.setText(sAccCurr);
                    			population.setText(result[1]);
                    			longtitude.setText(result[2]);
                    			latitude.setText(result[3]);
                            sleep(5000);
     
                            } catch (Exception e) { }
                            // Dismiss the Dialog 
                            myProgressDialog.dismiss();
                    }
     };
     
    thread.start();

  7. #7
    Membre chevronné
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    322
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 322
    Par défaut
    Rajoutes un Log dans le catch pour voir si tu ne passes pas à l’intérieur.

  8. #8
    Membre confirmé
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mars 2011
    Messages
    58
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2011
    Messages : 58
    Par défaut Probleme progress dialog
    Voici mon code complet
    ca marche toujours pas
    j'ai ajouté asynctask


    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
     
    public class ProgressBarAppli extends Activity implements OnClickListener, OnItemSelectedListener{
     
    	ProgressDialog myProgressDialog;    
    	String sAccCurr ;
    	String chaine="Agadir";
    	TextView ville;
    	TextView population;
    	TextView longtitude;
    	TextView latitude;
    	String[] result;
    	@Override
    	public void onCreate(Bundle icicle) {
    		super.onCreate(icicle);
    		setContentView(R.layout.main);
     
    		String myurl="";
    		URL url;
    		try {
    			url = new URL(myurl);
    			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			Log.i("ereur",e.getMessage());
    		}
     
     
    		Spinner s = (Spinner) findViewById(R.id.spinner);    
    		ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item);    
    		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
    		s.setAdapter(adapter);
     
    		s.setOnItemSelectedListener(this);
     
     
     
    	}	
    	private String getHttpResponse(String ville) {
    		String result = null;
    		URL url = null;
    		// Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " location = " + location)
    		try {
    			url = new URL("http://testandroid.hebergratuit.com/index.php?ville=\""+ville+"\"");
    			//   Log.d(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url = " + url);
    		} catch (MalformedURLException e) {
    			//  Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
    		}
     
    		if (url != null) {
    			try {
    				HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    				BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    				String inputLine;
     
    				int lineCount = 1; // limit the lines for the example
    				while ((lineCount < 10) && ((inputLine = in.readLine()) != null)) {
    					lineCount++;
    					//  Log.v(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " inputLine = " + inputLine);
     
    					result += "\n" + inputLine;
    				}
     
    				in.close();
    				urlConn.disconnect();
     
    			} catch (IOException e) {
    				// Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " " + e.getMessage());
    			}
    		} else {
    			//  Log.e(Constants.LOGTAG, " " + SimpleGet.CLASSTAG + " url NULL");
    		}
    		return result;
    	}
     
     
    	public String[] splitResult(String result){
     
    		String[] resultat =new String[4];
     
    		resultat=result.split(":");
     
    		return resultat;
    	}
     
     
     
    	@Override
    	public void onClick(View arg0) {
     
     
    	}
    	@Override
    	public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
    			long arg3) {
     
     
     
     
    		ville=(TextView)findViewById(R.id.ville);
    		population=(TextView)findViewById(R.id.population);
    		longtitude=(TextView)findViewById(R.id.longtitude);
    		latitude=(TextView)findViewById(R.id.latitude);
    		Spinner AccCurr = (Spinner) findViewById(R.id.spinner); 
    		sAccCurr = AccCurr.getSelectedItem().toString();
     
     
    		LoadTask load=new LoadTask();
    		load.execute(result);
     
     
     
     
     
     
    	}
     
     
    	class LoadTask extends AsyncTask<String ,String , String> {  
     
     
            @Override  
            protected String doInBackground(String... urls) {
     
        		chaine=getHttpResponse(sAccCurr);
        		result=splitResult(chaine);
     
        		ville.setText(sAccCurr);
        		population.setText(result[1]);
        		longtitude.setText(result[2]);
        		latitude.setText(result[3]);
     
            	return chaine;  
     
            }  
     
              protected void onProgressUpdate(Void inutilise) {  
     
         		 myProgressDialog = ProgressDialog.show(ProgressBarAppli.this, 
         	                "Please wait...", "Doing Extreme Calculations...", true);
     
              }  
            protected void onPostExecute(Void inutilise) {  
     
        		myProgressDialog.dismiss();
     
            }
     
     
        }
     
     
    	@Override
    	public void onNothingSelected(AdapterView<?> arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    }


    J'ai l'erreur suivant

    Only the original thread that create a view hierarchy can touch its views

  9. #9
    Membre chevronné
    Profil pro
    Inscrit en
    Mars 2011
    Messages
    322
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2011
    Messages : 322
    Par défaut
    Bonjour,
    Je ne suis pas un expert donc attends la confirmation d'une personne expérimentée.
    Pour moi il n'y a que le thread Ui qui peut mettre à jour l'affichage. Or tu fais cette mise à jour dans le thread que tu as crée et qui lui est censé faire les opérations longues. Quand tu veux faire une mise à jour tu appelles la fonction publishProgress qui elle même appellera une fonction gérée par l'Ui thread. Voici un exemple :

    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
     
     
    public void update() {
    //mise à jour de l'écran
    }
     
    class LoadTask extends AsyncTask<String ,String , String> {  
            @Override  
            protected String doInBackground(String... urls) {
     
        		//opération longue
                    publishProgress();
        	        //opération longue
                    publishProgress();
            	return chaine;  
     
            }  
              protected void onProgressUpdate(Void inutilise) {  
            	update();
              }  
        }

  10. #10
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,


    Pour moi il n'y a que le thread Ui qui peut mettre à jour l'affichage.
    http://developer.android.com/resourc...threading.html

    At first, this code seems to be a good solution to your problem, as it does not block the UI thread. Unfortunately, it violates the single-threaded model for the UI: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. In this piece of code above, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming.

Discussions similaires

  1. Probleme Boite Dialog DoModal
    Par neoangelo5 dans le forum MFC
    Réponses: 1
    Dernier message: 30/06/2011, 11h42
  2. Probleme Progress Dialog
    Par medi88 dans le forum Android
    Réponses: 15
    Dernier message: 25/03/2011, 12h07
  3. Dismiss Progress Dialog et Activity en meme temps
    Par Viish dans le forum Composants graphiques
    Réponses: 6
    Dernier message: 20/11/2010, 18h59
  4. problem occurred dialog box
    Par croc14 dans le forum SWT/JFace
    Réponses: 5
    Dernier message: 01/12/2009, 20h01
  5. [VB6]Probleme Common Dialog
    Par ganon551 dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 12/02/2006, 12h04

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