Bonjour,
Je développe actuellement une application Android qui génère un formulaire.
Pour générer se formulaire je crée beaucoup de composants graphiques (LinearLayout, EditText, TexView, RadioGroup...).
La génération du formulaire me pose des soucis d'attente dans le thread principal, il se fige.
Je souhaite donc faire cette génération dans une AsyncTask.
N'ayant jamais utilisé d'autre Thread ou d'AsyncTask je n'ai peut être pas toutes les carte en mains pour comprendre d'ou viens l'erreur.
Quand je procède de cette manière l'application plante sur la ligne qui instancie un EditText dans le controleur de vue.
Auriez-vous quelques renseignement pour m'aider
Merci.
l'erreur en question :
08-19 16:09:09.019 21314-21533/com.gecosdroid E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.gecosdroid, PID: 21314
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.sec.clipboard.ClipboardExManager$1.<init>(ClipboardExManager.java:123)
at android.sec.clipboard.ClipboardExManager.<init>(ClipboardExManager.java:123)
at android.app.ContextImpl$11.createService(ContextImpl.java:473)
at android.app.ContextImpl$ServiceFetcher.getService(ContextImpl.java:365)
at android.app.ContextImpl.getSystemService(ContextImpl.java:2079)
at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:117)
at android.app.Activity.getSystemService(Activity.java:4825)
at android.widget.EditText.<init>(EditText.java:81)
at android.widget.EditText.<init>(EditText.java:75)
at android.widget.EditText.<init>(EditText.java:71)
at com.gecosdroid.View.ViewCtrlFormulaire.genererEditText(ViewCtrlFormulaire.java:126)
at com.gecosdroid.Controler.CtrlFormulaire.getComposant(CtrlFormulaire.java:447)
at com.gecosdroid.Controler.CtrlFormulaire.access$600(CtrlFormulaire.java:41)
at com.gecosdroid.Controler.CtrlFormulaire$GenererFormulaire.doInBackground(CtrlFormulaire.java:278)
at com.gecosdroid.Controler.CtrlFormulaire$GenererFormulaire.doInBackground(CtrlFormulaire.java:205)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
************at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
************at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
************at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
************at java.lang.Thread.run(Thread.java:841)
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 // ASYNCTASK private class GenererFormulaire extends AsyncTask<Void, Integer, Void> { private ProgressBar mProgressBar = (ProgressBar) CtrlFormulaire.this._actForm.findViewById(R.id.pBAsync); @Override protected void onPreExecute() { super.onPreExecute(); Toast.makeText(CtrlFormulaire.this._actForm, "Début du traitement asynchrone", Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(Integer... values){ super.onProgressUpdate(values); // Mise à jour de la ProgressBar mProgressBar.setProgress(values[0]); } @Override protected void onCancelled(){ super.onCancelled(); // traitement à effectuer si la tâche est annulée } @Override protected Void doInBackground(Void... arg0) { Question que; Reponse rep; LinearLayout llSheet; LinearLayout llQue; LinearLayout llContRep = null; LinearLayout llRep; LinearLayout llSeprateur; TextView txtQue; TextView txtInf; for (int i = 0; i < lstSheets.size(); i++) { llSheet = _vCtrlForm.genererLinearLayoutVerticale(i); String nomSheet = lstSheets.get(i); for (int j = 0; j < lstQuestions.size(); j++) { int compteur = 0; que = lstQuestions.get(j); String s = que.getLib_question(); String sheetQue = que.getSheet_question(); if (sheetQue.matches(nomSheet)) { llQue = _vCtrlForm.genererLinearLayoutVerticale(j); llSeprateur = _vCtrlForm.genererLinearLayoutSeparateur(j); txtQue = _vCtrlForm.genererTextViewQuestion(j, s); llQue.addView(txtQue); for (int k = 0; k < lstReponses.size(); k++) { if ((compteur % 4) == 0) { llContRep = _vCtrlForm.genererLinearLayoutHorizontale(compteur); } rep = lstReponses.get(k); if (rep.getId_question() == que.getId_question()) { llRep = _vCtrlForm.genererLinearLayoutReponse(k); int idListe = rep.getId_liste(); String libRep = rep.getLib_reponse(); View v = getComposant(idListe, k, libRep); int idInformation = rep.getId_information(); im.open(); Information info = im.getInformation(idInformation); im.close(); String information = info.getLib_information(); txtInf = _vCtrlForm.genererTextViewInfo(j, information); llRep.addView(txtInf); if (v != null) { llRep.addView(v); lstComposants.add(v); } llContRep.addView(llRep); compteur++; } if ((compteur % 4) == 0) { llQue.addView(llContRep); } } if ((compteur % 4) != 0) { llQue.addView(llContRep); } llSheet.addView(llQue); llSheet.addView(llSeprateur); } } lstLLayoutSheet.add(llSheet); } return null; } @Override protected void onPostExecute(Void result) { Toast.makeText(CtrlFormulaire.this._actForm, "Le traitement asynchrone est terminé", Toast.LENGTH_LONG).show(); initialiserComposantFormulaire(); remplirSpinnerNavigation(); } }
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 // METHODES DANS LE CONTROLEUR DE VUE public LinearLayout genererLinearLayoutHorizontale(int idLayout) { LinearLayout ll = new LinearLayout(this._actForm); ll.setId(idLayout); ll.setOrientation(LinearLayout.HORIZONTAL); ll.isVerticalScrollBarEnabled(); return ll; } public RadioGroup genererRadioGroup(ArrayList<Choix> lstChx, int idReponse, String reponse) { RadioGroup rg = new RadioGroup(this._actForm); rg.setOrientation(LinearLayout.HORIZONTAL); rg.setId(idReponse); for (int i = 0; i < lstChx.size(); i++) { String libChx = lstChx.get(i).getLib_choix(); RadioButton rb = new RadioButton(this._actForm); rb.setId(i); rb.setText(libChx); if (reponse.equals(libChx)) { rb.setChecked(true); } rg.addView(rb); } return rg; } public Spinner genererSpinner(ArrayList<Choix> lstChx, int idReponse, String reponse) { Spinner sp = new Spinner(this._actForm); sp.setBackgroundResource(R.drawable.spinner_choix); sp.setId(idReponse); Spinner.LayoutParams lp = new Spinner.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40); sp.setLayoutParams(lp); Choix chx; ArrayList<String> lstChxString = new ArrayList<>(); ArrayAdapter<String> adChoix; for (int i = 0; i < lstChx.size(); i++) { chx = lstChx.get(i); lstChxString.add(chx.getLib_choix()); } adChoix = new ArrayAdapter<>(this._actForm, android.R.layout.simple_list_item_1, lstChxString); sp.setAdapter(adChoix); for (int i = 0; i < lstChx.size(); i++) { chx = lstChx.get(i); if (reponse.equals(chx.getLib_choix())) { sp.setSelection(i); } } return sp; } public EditText genererEditText (int idReponse, String reponse) { EditText et = new EditText(this._actForm); et.setId(idReponse); et.setSingleLine(); et.setText(reponse); return et; } public TextView genererTextViewQuestion (int idReponse, String reponse) { TextView tv = new TextView(this._actForm); tv.setTextAppearance(this._actForm, R.style.question_texte); tv.setId(idReponse); tv.setText(reponse); return tv; } public TextView genererTextViewInfo (int idReponse, String reponse) { TextView tv = new TextView(this._actForm); tv.setTextAppearance(this._actForm, R.style.information_texte); tv.setId(idReponse); tv.setText(reponse); return tv; } public LinearLayout genererLinearLayoutVerticale(int idLayout) { LinearLayout ll = new LinearLayout(this._actForm); ll.setId(idLayout); ll.setOrientation(LinearLayout.VERTICAL); ll.isVerticalScrollBarEnabled(); return ll; } public LinearLayout genererLinearLayoutSeparateur(int idLayout) { LinearLayout ll = new LinearLayout(this._actForm); ll.setId(idLayout); ll.setOrientation(LinearLayout.VERTICAL); ll.setBackgroundResource(R.color.barre_separation); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 3); lp.bottomMargin = 10; lp.topMargin = 10; ll.setLayoutParams(lp); return ll; } public LinearLayout genererLinearLayoutReponse(int idLayout) { LinearLayout ll = new LinearLayout(this._actForm); ll.setId(idLayout); ll.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); lp.weight = 1; ll.setLayoutParams(lp); return ll; }
Partager