Bonjour à tous,
je rencontre lors de l'exécution d'une application que je suis en train de développer l'erreur suivante lorsque j'envoie le nom d'une recette à l'activité chargée de me la présenter:
Les .java que j'exécute sont :
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 01-13 00:52:02.394: E/AndroidRuntime(20698): FATAL EXCEPTION: main 01-13 00:52:02.394: E/AndroidRuntime(20698): Process: com.example.livrerecettes, PID: 20698 01-13 00:52:02.394: E/AndroidRuntime(20698): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.livrerecettes/com.example.livrerecettes.AfficherRecette}: java.lang.NullPointerException 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread.access$800(ActivityThread.java:135) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.os.Handler.dispatchMessage(Handler.java:102) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.os.Looper.loop(Looper.java:136) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread.main(ActivityThread.java:5017) 01-13 00:52:02.394: E/AndroidRuntime(20698): at java.lang.reflect.Method.invokeNative(Native Method) 01-13 00:52:02.394: E/AndroidRuntime(20698): at java.lang.reflect.Method.invoke(Method.java:515) 01-13 00:52:02.394: E/AndroidRuntime(20698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 01-13 00:52:02.394: E/AndroidRuntime(20698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 01-13 00:52:02.394: E/AndroidRuntime(20698): at dalvik.system.NativeStart.main(Native Method) 01-13 00:52:02.394: E/AndroidRuntime(20698): Caused by: java.lang.NullPointerException 01-13 00:52:02.394: E/AndroidRuntime(20698): at com.example.livrerecettes.RecetteBDD.getRecetteWithName(RecetteBDD.java:94) 01-13 00:52:02.394: E/AndroidRuntime(20698): at com.example.livrerecettes.AfficherRecette.onCreate(AfficherRecette.java:27) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.Activity.performCreate(Activity.java:5231) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 01-13 00:52:02.394: E/AndroidRuntime(20698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 01-13 00:52:02.394: E/AndroidRuntime(20698): ... 11 more
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 package com.example.livrerecettes; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.support.v4.app.NavUtils; public class ChercherRecettes extends Activity { private final static String EXTRA_NAME = "namereceipe"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chercherrecettes); // Show the Up button in the action bar. setupActionBar(); } /** * Set up the {@link android.app.ActionBar}. */ private void setupActionBar() { getActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.chercher_recettes, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void Chercher(View view){ EditText recettename = (EditText) findViewById(R.id.recettename); Intent intent = new Intent(this, AfficherRecette.class); intent.putExtra(com.example.livrerecettes.ChercherRecettes.EXTRA_NAME,recettename.getText().toString()); startActivity(intent); } }
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 package com.example.livrerecettes; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.RatingBar; import android.support.v4.app.NavUtils; public class AfficherRecette extends Activity { private final static String EXTRA_NAME = "namereceipe"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_afficherrecette); // Show the Up button in the action bar. setupActionBar(); RecetteBDD recetteBdd = new RecetteBDD(this); Intent intent = getIntent(); Recette recette = new Recette(); recette = recetteBdd.getRecetteWithName(intent.getStringExtra(EXTRA_NAME)); EditText name = (EditText) findViewById(R.id.name); RatingBar note = (RatingBar) findViewById(R.id.note); EditText preparation = (EditText) findViewById(R.id.preparation); EditText cuisson = (EditText) findViewById(R.id.cuisson); EditText quantity = (EditText) findViewById(R.id.quantity); EditText ingredients = (EditText) findViewById(R.id.ingredients); EditText receipe = (EditText) findViewById(R.id.receipe); EditText commentaire = (EditText) findViewById(R.id.commentaire); name.setText(recette.getName()); note.setRating(recette.getNote()); preparation.setText(recette.getPreparation()); cuisson.setText(recette.getCuisson()); quantity.setText(recette.getQuantite()); ingredients.setText(recette.getIngredients()); receipe.setText(recette.getRecette()); commentaire.setText(recette.getCommentaire()); recetteBdd.close(); } /** * Set up the {@link android.app.ActionBar}. */ private void setupActionBar() { getActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.afficher_recette, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void ok (View view){ Intent intent = new Intent(this, MainActivity.class); startActivity(intent); } }
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 package com.example.livrerecettes; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class RecetteBDD { private static final int DATABASE_VERSION = 1; private static final String NOM_BDD = "recettes.db"; private static final String TABLE_RECETTE = "table_recettes"; private static final String COL_ID = "ID"; private static final int NUM_COL_ID = 0; private static final String COL_NAME = "Nom"; private static final int NUM_COL_NAME = 1; private static final String COL_NOTE = "Note"; private static final int NUM_COL_NOTE = 2; private static final String COL_PREPARATION = "Preparation"; private static final int NUM_COL_PREPARATION = 3; private static final String COL_CUISSON = "Cuisson"; private static final int NUM_COL_CUISSON = 4; private static final String COL_QUANTITE = "Quantite"; private static final int NUM_COL_QUANTITE = 5; private static final String COL_INGREDIENTS = "Ingredients"; private static final int NUM_COL_INGREDIENTS = 6; private static final String COL_RECETTE = "Recette"; private static final int NUM_COL_RECETTE = 7; private static final String COL_COMMENTAIRE = "Commentaire"; private static final int NUM_COL_COMMENTAIRE = 8; private SQLiteDatabase bdd; private DatabaseHandler database; public RecetteBDD(Context context){ //Création de la base de données et de la table database = new DatabaseHandler(context, NOM_BDD,null, DATABASE_VERSION); } public void open(){ //On ouvre la BDD en écriture bdd = database.getWritableDatabase(); } public void close (){ //On ferme l'accès à la BDD bdd.close(); } public SQLiteDatabase getBDD(){ return bdd; } public long insertRecette(Recette recette){ //Création d'un ContentValues ContentValues values = new ContentValues(); values.put(COL_NAME, recette.getName()); values.put(COL_NOTE, recette.getNote()); values.put(COL_PREPARATION, recette.getPreparation()); values.put(COL_CUISSON, recette.getCuisson()); values.put(COL_QUANTITE, recette.getQuantite()); values.put(COL_INGREDIENTS, recette.getIngredients()); values.put(COL_RECETTE, recette.getRecette()); values.put(COL_COMMENTAIRE, recette.getCommentaire()); return bdd.insert(TABLE_RECETTE, null, values); } //Pour mettre à jour une recette, inutilisé pour le moment public int updateRecette (int id, Recette recette){ ContentValues values = new ContentValues(); values.put(COL_NAME, recette.getName()); values.put(COL_NOTE, recette.getNote()); values.put(COL_PREPARATION, recette.getPreparation()); values.put(COL_CUISSON, recette.getCuisson()); values.put(COL_QUANTITE, recette.getQuantite()); values.put(COL_INGREDIENTS, recette.getIngredients()); values.put(COL_RECETTE, recette.getRecette()); values.put(COL_COMMENTAIRE, recette.getCommentaire()); return bdd.update(TABLE_RECETTE, values, COL_ID + " = " + id, null); } //Suppression d'une recette public int removeRecette(int id){ return bdd.delete(TABLE_RECETTE, COL_ID + " = " + id, null); } public Recette getRecetteWithName(String name){ Cursor c = bdd.query(TABLE_RECETTE, new String[] {COL_ID, COL_NAME, COL_NOTE, COL_PREPARATION, COL_CUISSON, COL_QUANTITE, COL_INGREDIENTS, COL_RECETTE, COL_COMMENTAIRE}, COL_NAME + " LIKE \"" + name + "\"", null, null, null, null); return cursorToRecette(c); } public Recette[] getAllNameRecette(){ Cursor c = bdd.query(TABLE_RECETTE, new String[] {COL_NAME}, null, null, null, null, null); return cursorToAllRecette(c); } private Recette[] cursorToAllRecette(Cursor c) { // Si aucune recette n'a été trouvée if (c.getCount() == 0){ return null; } //On se place sur le premier élément c.moveToFirst(); Recette listerecette[] = {}; while(c.moveToNext()){ Recette recette = new Recette(); recette.setName(c.getString(NUM_COL_NAME)); } c.close(); return listerecette; } private Recette cursorToRecette(Cursor c) { //si aucune recette n'a été trouvée, on renvoie null if (c.getCount() == 0){ return null; } //Sinon on se place sur le premier élément c.moveToFirst(); //on crée une nouvelle recette Recette recette = new Recette(); //on lui affecte les infos contenues dans le Cursor recette.setId(c.getInt(NUM_COL_ID)); recette.setName(c.getString(NUM_COL_NAME)); recette.setNote(c.getInt(NUM_COL_NOTE)); recette.setPreparation(c.getString(NUM_COL_PREPARATION)); recette.setCuisson(c.getString(NUM_COL_CUISSON)); recette.setQuantite(c.getString(NUM_COL_QUANTITE)); recette.setIngredients(c.getString(NUM_COL_INGREDIENTS)); recette.setRecette(c.getString(NUM_COL_RECETTE)); recette.setCommentaire(c.getString(NUM_COL_COMMENTAIRE)); //On ferme le cursor c.close(); //On retrourne la recette return recette; } /* *import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHandler extends SQLiteOpenHelper{ //version de la base de données (en cas de modifications) private static final int DATABASE_VERSION = 1; //Nom de la table et des différentes colonnes private static final String TABLE_RECETTE = "table_recettes"; private static final String COL_ID = "ID"; private static final String COL_NAME = "Nom"; private static final String COL_NOTE = "Note"; private static final String COL_PREPARATION = "Preparation"; private static final String COL_CUISSON = "Cuisson"; private static final String COL_QUANTITE = "Quantite"; private static final String COL_INGREDIENTS = "Ingredients"; private static final String COL_RECETTE = "Recette"; private static final String COL_COMMENTAIRE = "Commentaire"; //Création de la table private static final String CREATE_BDD = "CREATE TABLE " + TABLE_RECETTE + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_NAME + " TEXT NOT NULL, " + COL_NOTE + " REAL, " + COL_PREPARATION + " TEXT NOT NULL, " + COL_CUISSON + " TEXT NOT NULL, " + COL_QUANTITE + " TEXT NOT NULL, " + COL_INGREDIENTS + " TEXT NOT NULL, " + COL_RECETTE + " TEXT NOT NULL, " + COL_COMMENTAIRE + " TEXT);"; public DatabaseHandler (Context context, String name, CursorFactory factory, int version){ super(context, name, factory, version); } // Création de la base de données @Override public void onCreate(SQLiteDatabase db){ db.execSQL(CREATE_BDD); } //En cas de mise à jour de la base de données, on supprime la table et on la crét à nouveau @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("DROP TABLE " + TABLE_RECETTE +";"); onCreate(db); */ }
Merci d'avance pour votre aide![]()
Partager