Bonjour,
Je suis un vrai débutant en Android, je conçois une application me permettant
de gérer mes dépenses.
J'arrive bien à insérer des données d'un formulaire dans ma base mais
c'est l'affichage du contenu de la base dans une listview qui ne marche
pas.
Le fichier à problème s'appelle ListeDepensesActivity.java et il se lance
quand je clique sur le bouton enregistrer du layout main.xml. Le click
sur ce bouton insére les datas dans la base, et j'ai vérifié que les données
insérées sont OK. Après cette insertion, je démarre une intent qui me
lance le fichier ListeDepensesActivity qui est supposé m'afficher une
listeView (layout listedepenses.xml) avec les données de la base.
C'est ce dernier affichage qui me plante. Pouvez vous m'aider svp ?
Merci.
Voici mon main.xml
Mon fichier contenant les listview : row.xml
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/NouvelleDepense" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/DateDepense" /> <EditText android:id="@+id/ChampDateDepense" android:layout_width="fill_parent" android:layout_height="32dp" android:text="@string/ChampDateDepense" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Operation" /> <EditText android:id="@+id/ChampOperation" android:layout_width="fill_parent" android:layout_height="32dp" android:ems="10" android:text="@string/ChampOperation" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/categorie" /> <EditText android:id="@+id/ChampCategorie" android:layout_width="fill_parent" android:layout_height="32dp" android:ems="10" android:text="@string/ChampCategorie" > <requestFocus /> </EditText> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/moyenpaiement" /> <EditText android:id="@+id/ChampMoyenPaiement" android:layout_width="fill_parent" android:layout_height="32dp" android:ems="10" android:text="@string/ChampMoyenPaiement" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/SommeDepensee" /> <EditText android:id="@+id/ChampSommeDepensee" android:layout_width="fill_parent" android:layout_height="32dp" android:ems="10" android:text="@string/ChampSommeDepensee" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Commentaire" /> <EditText android:id="@+id/ChampCommentaire" android:layout_width="fill_parent" android:layout_height="32dp" android:ems="10" android:text="@string/ChampCommentaire" /> <Button android:id="@+id/ButtonAjouter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/Enregistrer" /> <Button android:id="@+id/ButtonSupprimerTout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/supprimertout" /> </LinearLayout>
Fichier layout pour l'affichage du contenu : listedepenses.xml
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:text="#"/> <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:paddingRight="10dip"/> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:paddingRight="10dip" android:text="-" /> <TextView android:id="@+id/text1" android:layout_width="53dp" android:layout_height="fill_parent" android:padding="2dip" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:padding="2dip" android:paddingRight="10dip" android:text="-" /> <TextView android:id="@+id/text2" android:layout_width="106dp" android:layout_height="wrap_content" android:padding="2dip" /> </LinearLayout> </LinearLayout>
L'activité Depense.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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/ListeDepenses" android:textAppearance="?android:attr/textAppearanceLarge" /> <ListView android:id="@+id/contentlist" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
L'activité d'affichage : ListeDepenseActivity.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104 package fr.toto.AssistantPersonnel; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class Depense extends Activity { EditText ChampDateDepense, ChampOperation, ChampMoyenPaiement, ChampSommeDepensee, ChampCategorie, ChampCommentaire; Button BoutonAjouter, BoutonSupprimerTout; private SQLiteAdapter mySQLiteAdapter; ListView listContent; SimpleCursorAdapter cursorAdapter; Cursor cursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //Les différents champs à remplir ChampDateDepense = (EditText)findViewById(R.id.ChampDateDepense); ChampOperation = (EditText)findViewById(R.id.ChampOperation); ChampMoyenPaiement = (EditText)findViewById(R.id.ChampMoyenPaiement); ChampSommeDepensee = (EditText)findViewById(R.id.ChampSommeDepensee); ChampCategorie = (EditText)findViewById(R.id.ChampCategorie); ChampCommentaire = (EditText)findViewById(R.id.ChampCommentaire); //Boutons ajouter et supprimer tout BoutonAjouter = (Button)findViewById(R.id.ButtonAjouter); BoutonSupprimerTout = (Button)findViewById(R.id.ButtonSupprimerTout); //setContentView(R.layout.listedepenses); //Instanciation de la base MySQL mySQLiteAdapter = new SQLiteAdapter(this); mySQLiteAdapter.openToWrite(); //Association d'un mode d'écoute "OnClick" aux boutons BoutonAjouter.setOnClickListener(buttonAddOnClickListener); BoutonSupprimerTout.setOnClickListener(buttonDeleteAllOnClickListener); } Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener(){ //Méthode qui se déclenche au click bouton public void onClick(View arg0) { // TODO Auto-generated method stub String data1 = ChampDateDepense.getText().toString(); String data2 = ChampOperation.getText().toString(); String data3 = ChampMoyenPaiement.getText().toString(); String data4 = ChampSommeDepensee.getText().toString(); String data5 = ChampCategorie.getText().toString(); String data6 = ChampCommentaire.getText().toString(); mySQLiteAdapter.insert(data1, data2, data3, data4, data5, data6); Toast.makeText(getApplicationContext(), "1 ligne inserée", 7).show(); Intent i = new Intent(getApplicationContext(), ListeDepenseActivity.class); startActivity(i); } }; Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener(){ public void onClick(View arg0) { // TODO Auto-generated method stub mySQLiteAdapter.deleteAll(); Toast.makeText(getApplicationContext(), "Tout est supprimé", 7).show(); } }; @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); mySQLiteAdapter.close(); } /*private void updateList(){ cursor.requery(); }*/ }
L'activité MySQL : SQLiteAdapter.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 package fr.toto.AssistantPersonnel; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.content.Intent; import android.database.Cursor; public class ListeDepenseActivity extends Activity { public static final String MYDATABASE_NAME = "AssistantPersonnel"; public static final String MYDATABASE_TABLE = "Depense"; public static final int MYDATABASE_VERSION = 1; public static final String KEY_ID = "_id"; public static final String KEY_CONTENT1 = "datedepense"; public static final String KEY_CONTENT2 = "operation"; public static final String KEY_CONTENT3 = "moyenpaiement"; public static final String KEY_CONTENT4 = "sommedepensee"; public static final String KEY_CONTENT5 = "categorie"; public static final String KEY_CONTENT6 = "commentaire"; private SQLiteAdapter mySQLiteAdapter; SimpleCursorAdapter cursorAdapter; Cursor cursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listedepenses); //Liste d'affichage des données issues de la base ListView listContent = (ListView)findViewById(R.id.contentlist); //Affichage des données en ligne cursor = mySQLiteAdapter.queueAll(); updateList(); String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3, SQLiteAdapter.KEY_CONTENT4, SQLiteAdapter.KEY_CONTENT5, SQLiteAdapter.KEY_CONTENT6}; //String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2}; int[] to = new int[]{R.id.id, R.id.text1, R.id.text2}; cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); listContent.setAdapter(cursorAdapter); } private void updateList(){ cursor.requery(); } }
Le TOMCAT me dit :
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 package fr.toto.AssistantPersonnel; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class SQLiteAdapter { public static final String MYDATABASE_NAME = "AssistantPersonnel"; public static final String MYDATABASE_TABLE = "Depense"; public static final int MYDATABASE_VERSION = 1; public static final String KEY_ID = "_id"; public static final String KEY_CONTENT1 = "datedepense"; public static final String KEY_CONTENT2 = "operation"; public static final String KEY_CONTENT3 = "moyenpaiement"; public static final String KEY_CONTENT4 = "sommedepensee"; public static final String KEY_CONTENT5 = "categorie"; public static final String KEY_CONTENT6 = "commentaire"; //create table MY_DATABASE (ID integer primary key, Content text not null); private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null, " + KEY_CONTENT4 + " text not null, " + KEY_CONTENT5 + " text not null, " + KEY_CONTENT6 + " text not null);"; private SQLiteHelper sqLiteHelper; private SQLiteDatabase sqLiteDatabase; private Context context; public SQLiteAdapter(Context c){ context = c; } //Ouverture pour lecture de la bdd public SQLiteAdapter openToRead() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; } //Ouverture pour ecriture de la bdd public SQLiteAdapter openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; } public void close(){ sqLiteHelper.close(); } public long insert(String content1, String content2, String content3, String content4, String content5, String content6){ ContentValues contentValues = new ContentValues(); contentValues.put(KEY_CONTENT1, content1); contentValues.put(KEY_CONTENT2, content2); contentValues.put(KEY_CONTENT3, content3); contentValues.put(KEY_CONTENT4, content4); contentValues.put(KEY_CONTENT5, content5); contentValues.put(KEY_CONTENT6, content6); return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); } public int deleteAll(){ return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); } public Cursor queueAll(){ String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2, KEY_CONTENT3, KEY_CONTENT4, KEY_CONTENT5, KEY_CONTENT6 }; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null); return cursor; } public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } }
Merci pour votre aide.
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 09-26 12:22:29.285: E/AndroidRuntime(16033): FATAL EXCEPTION: main 09-26 12:22:29.285: E/AndroidRuntime(16033): java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.EdgarPetrus.AssistantPersonnel/fr.EdgarPetrus.AssistantPersonnel.ListeDepenseActivity}: java.lang.NullPointerException 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread.access$600(ActivityThread.java:127) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.os.Handler.dispatchMessage(Handler.java:99) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.os.Looper.loop(Looper.java:137) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread.main(ActivityThread.java:4507) 09-26 12:22:29.285: E/AndroidRuntime(16033): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 12:22:29.285: E/AndroidRuntime(16033): at java.lang.reflect.Method.invoke(Method.java:511) 09-26 12:22:29.285: E/AndroidRuntime(16033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 09-26 12:22:29.285: E/AndroidRuntime(16033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 09-26 12:22:29.285: E/AndroidRuntime(16033): at dalvik.system.NativeStart.main(Native Method) 09-26 12:22:29.285: E/AndroidRuntime(16033): Caused by: java.lang.NullPointerException 09-26 12:22:29.285: E/AndroidRuntime(16033): at fr.toto.AssistantPersonnel.ListeDepenseActivity.onCreate(ListeDepenseActivity.java:38) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.Activity.performCreate(Activity.java:4465) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052) 09-26 12:22:29.285: E/AndroidRuntime(16033): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
Partager