Bonjour,
voilà pour un projet j'ai besoin d'une base de donnée SQLITE mais je ne comprend pas pourquoi au lancement de mon application quand j'ajoute les commandes "database.ajouterPlayer();" mon app plante au lancement et se ferme tandis que quand je commente cette ligne (ainsi que database.close()) mon application se lance normalement et je peut effectuer les actions qui ont été programmé (sans accès à la base bien sur) quelqu'un saurait de quoi cela provient ? j'ai suivi un tuto et regarder les ressources et d'après moi il n'y a pas d'erreur dans mon code, peut-être ai-je loupé quelque chose ?
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 package com.example.never; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; public class NumberOfPlayers extends AppCompatActivity { private Button btnAdd; public TextView P1, P2, P3, P4, P5; private Database database; private ViewGroup layoutAddPlayer; private int id = 1; private EditText playerName; private LinearLayout.LayoutParams config; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_number_of_players); P1 = (TextView) findViewById(R.id.P1); P2 = (TextView) findViewById(R.id.P2); P3 = (TextView) findViewById(R.id.P3); P4 = (TextView) findViewById(R.id.P4); P5 = (TextView) findViewById(R.id.P5); database = new Database(this); layoutAddPlayer = findViewById(R.id.addPlayerLayout); btnAdd = findViewById(R.id.btnAdd); for (int i = 0; i < 4; i++){ playerName = new EditText(this); playerName.setId(id); playerName.setHint("Joueur n°"+playerName.getId()); config = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); config.setMargins(0,8,0,8); layoutAddPlayer.addView(playerName,config); database.ajouterPlayer(); id++; } database.close(); } public void addPlayer(View view) { playerName = new EditText(this); playerName.setId(id); playerName.setHint("Joueur n°"+playerName.getId()); config = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); config.setMargins(0,8,0,8); layoutAddPlayer.addView(playerName,config); // database.addPlayer(); id++; } public void play(View view) { /*for (int i = 0; i < id; i++) { }*/ } }
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 package com.example.jenaijamais; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class Database extends SQLiteOpenHelper { private static final String DATABASE_NAME = "Never.db"; private static final int DATABASE_VERSION = 1; public Database(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String PLAYER_TABLE = "CREATE TABLE players(" + "playerID integer primary key autoincrement," + "playerName text," + "playerScore integer not null)"; String QUESTION_TABLE = "CREATE TABLE questions(" + "questionID integer primary key autoincrement," + "questionTag text not null)"; String GAMELOOP_TABLE = "CREATE TABLE gameLoop(" + "gameQuestionID integer primary key autoincrement," + "questionID integer not null," + "questionPlay boolean)"; db.execSQL(PLAYER_TABLE);db.execSQL(QUESTION_TABLE);db.execSQL(GAMELOOP_TABLE); Log.i("DATABASE", "onCreate invoked"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void ajouterPlayer(){ String sqlRequest = "INSERT INTO players(playerName, playerScore) VALUES" + "(playerName = null,score = 0)"; this.getWritableDatabase().execSQL(sqlRequest); Log.i("DATABASE", "addPlayer invoked"); } }
Partager