Bonjour à tous,
Ça fais quelque jours que j'ai commencé à développer sous android, et aujourd'hui j'ai un problème que je n'arrive pas à résoudre.
Voici mon problème: j'utilise ma classe graphique "Chargement.java" qui permet d'afficher graphiquement (Activity) l'état d'avancement du thread qui tourne en fond de tache.
Dans mon Thread en fond de tache je simule le travail par des sleep(); et j'envoi des messages à l'aide du Handler.
Mais lorsque je traite le message dans la fonction "traitement_message(msg)" dans la classe "Chargement.java" je tente de faire une setText(); sur mes textView mais j'ai une erreur Fatal et l'application se ferme
Mon interface xml chargement.xml:
voici ma classe "Chargement.java":
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 <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/chargement" android:columnCount="1" > <TextView android:id="@+id/id_view_chargement" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_column="0" android:layout_gravity="left|center_vertical" android:layout_row="1" android:gravity="center" android:text="initialisation ..." /> <ProgressBar android:id="@+id/id_progress_chargement" android:layout_column="0" android:layout_gravity="center" android:layout_row="1" /> </GridLayout>
et ma classe de ThreadTravail.java:
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 package com.example.barryplayer; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.widget.ProgressBar; import android.widget.TextView; public class Chargement extends Activity { //=========================================== // Variables //=========================================== TextView text_chargement; ProgressBar progress_chargement ; // Code retour thread int CALCUL_IP_BROAD = 10; int RECHERCHE_RASPBERRY = 11; int CONNEXION_RASPBERRY = 12; int CONNECTE_RASPBERRY = 13; //=========================================== // Handler //=========================================== final Handler handler = new Handler(){ @Override public void handleMessage(Message msg){ // Traitement du message provenant du ThreadTravail traitement_message(msg); } }; //=========================================== // instenciation Classes //=========================================== ThreadTravail threadT = new ThreadTravail(handler); //=========================================== // onCreate //=========================================== @Override protected void onCreate(Bundle mBundle) { // TODO Auto-generated method stub super.onCreate(mBundle); // Initialisation de l'interface text_chargement = (TextView)findViewById(R.id.id_view_chargement); progress_chargement = (ProgressBar)findViewById(R.id.id_progress_chargement); // Affichage de l'interface setContentView(R.layout.chargement); } //=========================================== // onStart //=========================================== @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); // Execution du Thread de travail Thread thread1 = new Thread(threadT); thread1.start(); } //=========================================== // traitement_message(msg) //=========================================== public void traitement_message(Message msg){ if (msg.what == CALCUL_IP_BROAD){ text_chargement.setText("Calcul broadcast ..."); } else if (msg.what == RECHERCHE_RASPBERRY){ text_chargement.setText("Recherche raspberry ..."); } else if (msg.what == CONNEXION_RASPBERRY){ text_chargement.setText("Connexion raspberry ..."); } else if (msg.what == RECHERCHE_RASPBERRY){ text_chargement.setText("connecté !"); progress_chargement.setVisibility(progress_chargement.INVISIBLE); } } }
Voilà j'espère que je me suis bien exprimé
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 package com.example.barryplayer; import android.os.Handler; import android.util.Log; public class ThreadTravail implements Runnable { //=========================================== // Variables //=========================================== Handler mHandler; public ThreadTravail (Handler handler) { mHandler = handler; } @Override public void run() { // TODO Auto-generated method stub Log.d("smp", "Thread: Début du Thread ..."); try { Log.d("smp", "Thread: Simulation calcul ip Broadcast ..."); Thread.sleep(2000,0); mHandler.sendEmptyMessage(10); Log.d("smp", "Thread: Simulation recherche rasberry ..."); Thread.sleep(2000,0); mHandler.sendEmptyMessage(11); Log.d("smp", "Thread: Simulation connexion ..."); Thread.sleep(2000,0); mHandler.sendEmptyMessage(12); Log.d("smp", "Thread: Simulation connexion réussi !"); Thread.sleep(2000,0); mHandler.sendEmptyMessage(13); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }![]()
Partager