IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

2 BD SQLite Android


Sujet :

Android

  1. #1
    Membre du Club
    Homme Profil pro
    Developpeur Android
    Inscrit en
    Juin 2012
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur Android
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 65
    Points : 57
    Points
    57
    Par défaut 2 BD SQLite Android
    Bonsoir,
    ma question est simple ;

    Peut-on avoir 2 bases de données dans une application android ?
    C'est à dire dans le DDMS, avoir
    data > data > package > databases > db1.db
    data > data > package > databases > db2.db

    ??

    Je sais que c'est moche mais est-ce que ça fonctionne ?!

    Merci

  2. #2
    Membre éclairé

    Homme Profil pro
    Développeur mobile iOS / Android
    Inscrit en
    Décembre 2008
    Messages
    259
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur mobile iOS / Android
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 259
    Points : 690
    Points
    690
    Par défaut
    Bonjour,

    J'ai déjà remarqué qu'il y a des applications que j'ai installé dans mon mobile qui possédent deux bases de données en fouillant dans data/data/package/databases
    « Il est assez difficile de trouver une erreur dans son code quand on la cherche. C’est encore bien plus dur quand on est convaincu que le code est juste. » - Steve McConnell

    N'oubliez pas de consulter les FAQ Swift, Android
    Tutoriel : Développer une application multilingue sous iOS

  3. #3
    Membre éprouvé
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    757
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 757
    Points : 968
    Points
    968
    Par défaut
    Oui, rien ne t'empêche de créer plusieurs bases de données dans une application.

  4. #4
    Membre du Club
    Homme Profil pro
    Developpeur Android
    Inscrit en
    Juin 2012
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur Android
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 65
    Points : 57
    Points
    57
    Par défaut
    D'accord merci, je dois trouver pourquoi ça foire alors

    Je lance l'activity qui ne contient que cela :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class CoursesListe extends Activity {
     
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.liste);
     
            DBAdapterAliment dba = new DBAdapterAliment(this);
        }
    }
    L'erreur est donc sur le DBAdapterAliment.

    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
    06-25 10:11:21.656: W/dalvikvm(1059): Exception Ljava/lang/NullPointerException; thrown during Lcom/example/medichome/DBAdapterAliment;.<clinit>
    06-25 10:11:21.666: D/AndroidRuntime(1059): Shutting down VM
    06-25 10:11:21.666: W/dalvikvm(1059): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    06-25 10:11:21.666: E/AndroidRuntime(1059): Uncaught handler: thread main exiting due to uncaught exception
    06-25 10:11:21.676: E/AndroidRuntime(1059): java.lang.ExceptionInInitializerError
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.example.medichome.CoursesListe.onCreate(CoursesListe.java:30)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.os.Looper.loop(Looper.java:123)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.main(ActivityThread.java:4363)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at java.lang.reflect.Method.invokeNative(Native Method)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at java.lang.reflect.Method.invoke(Method.java:521)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at dalvik.system.NativeStart.main(Native Method)
    06-25 10:11:21.676: E/AndroidRuntime(1059): Caused by: java.lang.NullPointerException
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.example.medichome.DBAdapterAliment.<clinit>(DBAdapterAliment.java:55)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	... 14 more
    06-25 10:11:21.706: I/dalvikvm(1059): threadid=7: reacting to signal 3
    06-25 10:11:21.706: E/dalvikvm(1059): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
    06-25 10:11:23.099: I/Process(1059): Sending signal. PID: 1059 SIG: 9
    Voici le code de ma classe DBAdapterAliment :

    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
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    package com.example.medichome;
     
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.Log;
     
    public class DBAdapterAliment 
    { 
    	private static final int NUM_KEY = 0;
    	private static final int NUM_TITRE = 1;
    	private static final int NUM_IMAGE = 2;
    	private static final int NUM_DANSLIST = 3;
    	private static final int NUM_TYPE = 4;
     
        public static final String KEY_ROWID = "_id";
        public static final String KEY_TITRE = "titre";
        public static final String KEY_IMAGE = "image";
        public static final String KEY_DANSLIST = "danslist";
        public static final String KEY_TYPE = "type";
     
        private static final String TAG = "DBAdapterAliment";
        private static Context context;  
     
        private static final String DATABASE_NAME = "aliment_database.db";
        private static final String DATABASE_TABLE = "aliments";
        private static int DATABASE_VERSION = 1;
     
        public static final String titre1 = "\"Pommes\"";
        public static final String titre2 = "\"Oranges\"";
        public static final String titre3 = "\"Bananes\"";
        public static final String titre4 = "\"Fraises\"";
        public static final String titre5 = "\"Kiwis\"";
        public static final String titre6 = "\"Tomates\"";
        public static final String titre7 = "\"Haricots\"";
        public static final String titre8 = "\"Petit pois\"";
        public static final String titre9 = "\"Salade\"";
        public static final String titre10 = "\"Endives\"";
        public static final String titre11 = "\"Oeufs\"";
        public static final String titre12 = "\"Jambon\"";
        public static final String titre13 = "\"Poulet\"";
        public static final String titre14 = "\"Poisson\"";
        public static final String titre15 = "\"Boeuf\"";
        public static final String titre16 = "\"Eau\"";
        public static final String titre17 = "\"Sel\"";
        public static final String titre18 = "\"Poivre\"";
        public static final String titre19 = "\"Beurre\"";
        public static final String titre20 = "\"Lait\"";
     
        public static final Bitmap image1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.pomme);
        public static final Bitmap image2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.orange);
        public static final Bitmap image3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.banane);
        public static final Bitmap image4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.fraise);
        public static final Bitmap image5 = BitmapFactory.decodeResource(context.getResources(), R.drawable.kiwi);
        public static final Bitmap image6 = BitmapFactory.decodeResource(context.getResources(), R.drawable.tomate);
        public static final Bitmap image7 = BitmapFactory.decodeResource(context.getResources(), R.drawable.haricot);
        public static final Bitmap image8 = BitmapFactory.decodeResource(context.getResources(), R.drawable.petitp);
        public static final Bitmap image9 = BitmapFactory.decodeResource(context.getResources(), R.drawable.salade);
        public static final Bitmap image10 = BitmapFactory.decodeResource(context.getResources(), R.drawable.endive);
        public static final Bitmap image11 = BitmapFactory.decodeResource(context.getResources(), R.drawable.oeufs);
        public static final Bitmap image12 = BitmapFactory.decodeResource(context.getResources(), R.drawable.jambon);
        public static final Bitmap image13 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poulet);
        public static final Bitmap image14 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poisson);
        public static final Bitmap image15 = BitmapFactory.decodeResource(context.getResources(), R.drawable.boeuf);
        public static final Bitmap image16 = BitmapFactory.decodeResource(context.getResources(), R.drawable.eau);
        public static final Bitmap image17 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sel);
        public static final Bitmap image18 = BitmapFactory.decodeResource(context.getResources(), R.drawable.poivre);
        public static final Bitmap image19 = BitmapFactory.decodeResource(context.getResources(), R.drawable.beurre);
        public static final Bitmap image20 = BitmapFactory.decodeResource(context.getResources(), R.drawable.lait);
     
        public static final int type0 = 0;
        public static final int type1 = 1;
        public static final int type2 = 2;
        public static final int type3 = 3;
     
    	public static final String INSERT1 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre1 + "," + image1 + "1," + type0 + ");";
    	public static final String INSERT2 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre2 + "," + image2 + "1," + type0 + ");";
    	public static final String INSERT3 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre3 + "," + image3 + "1," + type0 + ");";
    	public static final String INSERT4 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre4 + "," + image4 + "1," + type0 + ");";
    	public static final String INSERT5 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre5 + "," + image5 + "1," + type0 + ");";
    	public static final String INSERT6 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre6 + "," + image6 + "1," + type1 + ");";
    	public static final String INSERT7 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre7 + "," + image7 + "1," + type1 + ");";
    	public static final String INSERT8 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre8 + "," + image8 + "1," + type1 + ");";
    	public static final String INSERT9 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre9 + "," + image9 + "1," + type1 + ");";
    	public static final String INSERT10 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre10 + "," + image10 + "1," + type1 + ");";
    	public static final String INSERT11 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre11 + "," + image11 + "1," + type2 + ");";
    	public static final String INSERT12 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre12 + "," + image12 + "1," + type2 + ");";
    	public static final String INSERT13 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre13 + "," + image13 + "1," + type2 + ");";
    	public static final String INSERT14 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre14 + "," + image14 + "1," + type2 + ");";
    	public static final String INSERT15 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre15 + "," + image15 + "1," + type2 + ");";
    	public static final String INSERT16 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre16 + "," + image16 + "1," + type3 + ");";
    	public static final String INSERT17 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre17 + "," + image17 + "1," + type3 + ");";
    	public static final String INSERT18 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre18 + "," + image18 + "1," + type3 + ");";
    	public static final String INSERT19 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre19 + "," + image19 + "1," + type3 + ");";
    	public static final String INSERT20 =  "INSERT INTO " + DATABASE_TABLE + " (titre, image, danslist, type) VALUES (" + titre20 + "," + image20 + "1," + type3 + ");";
     
    	private static final String DATABASE_CREATE =
            "create table aliments (_id integer primary key autoincrement, titre text not null,"
            + "image blob, danslist integer, type interger);";
     
        private DatabaseHelperAliment DBHelper;
        private SQLiteDatabase db;
     
        public DBAdapterAliment(Context ctx) 
        {
            this.context = ctx;
            DBHelper = new DatabaseHelperAliment(context);
        }
     
        private static class DatabaseHelperAliment extends SQLiteOpenHelper 
        {
        	DatabaseHelperAliment(Context context) 
            {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                System.out.println("DatabaseHelperAliment");
            }
     
            @Override
            public void onCreate(SQLiteDatabase db) 
            {
            	System.out.println(" OnCreate ");
                db.execSQL(DATABASE_CREATE);
                db.execSQL(INSERT1);
        		db.execSQL(INSERT2);
        		db.execSQL(INSERT3);
        		db.execSQL(INSERT4);
        		db.execSQL(INSERT5);
        		db.execSQL(INSERT6);
        		db.execSQL(INSERT7);
        		db.execSQL(INSERT8);
        		db.execSQL(INSERT9);
        		db.execSQL(INSERT10);
        		db.execSQL(INSERT11);
        		db.execSQL(INSERT12);
        		db.execSQL(INSERT13);
        		db.execSQL(INSERT14);
        		db.execSQL(INSERT15);
        		db.execSQL(INSERT16);
        		db.execSQL(INSERT17);
        		db.execSQL(INSERT18);
        		db.execSQL(INSERT19);
        		db.execSQL(INSERT20);
        		System.out.println(" 20 INSERT ");
        		Log.w("TEST : ", "PREMIERE CREATION DE LA BASE ALIMENT !");
            }
     
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
            {
            	System.out.println(" OnUpgrade ");
                Log.w(TAG, "Upgrading database from version " + oldVersion 
                      + " to "
                      + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS aliments");
                onCreate(db);
            }
        }    
     
        public DBAdapterAliment open() throws SQLException 
        {
        	//DATABASE_VERSION++;
        	db = DBHelper.getWritableDatabase();
            System.out.println(".open() OUVERTURE BD");
            return this;
        }
     
        //---closes the database---    
        public void close() 
        {
        	System.out.println(".close() FERMETURE BD");
            DBHelper.close();
        }
     
        //---insert a title into the database---
        public long insertAliment(String t, byte[] image, int dl, int ty) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_TITRE, t);
            initialValues.put(KEY_IMAGE, image);
            initialValues.put(KEY_DANSLIST, dl);
            initialValues.put(KEY_TYPE, ty);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }
     
        //---deletes a particular title---
        public boolean deleteAliment(long rowId) 
        {
        	boolean i = db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
        	if (i) System.out.println("Aliment supprimé");
            return i;
        }
     
        public boolean deleteAliment(String t) 
        {
        	boolean i = db.delete(DATABASE_TABLE, KEY_TITRE +" LIKE '" + t + "'", null) > 0;
        	if (i) System.out.println("Aliment supprimé");
            return i;
        }
     
        //---retrieves all the titles---
        public Cursor getAllAliment() 
        {
            return db.query(DATABASE_TABLE, new String[] {
            		KEY_ROWID, 
            		KEY_TITRE,
            		KEY_IMAGE,
            		KEY_DANSLIST, 
            		KEY_TYPE}, 
                    null, 
                    null, 
                    null, 
                    null, 
                    null);
        }
     
     
     
        //---retrieves a particular title---
        public Cursor getAliment(long rowId) throws SQLException 
        {
            Cursor mCursor =
                    db.query(true, DATABASE_TABLE, new String[] {
                    		KEY_ROWID, 
                    		KEY_TITRE,
                    		KEY_IMAGE,
                    		KEY_DANSLIST, 
                    		KEY_TYPE},
                    		KEY_ROWID + "=" + rowId, 
                    		null,
                    		null, 
                    		null, 
                    		null, 
                    		null);
            if (mCursor != null) {
                mCursor.moveToFirst();
            }
            return mCursor;
        }
     
        public Cursor getAllAlimentByDansList(int dl) throws SQLException 
        {
        	Cursor mCursor = db.rawQuery("select * from aliments where danslist = " + dl, null);
        	if (mCursor != null) {
                mCursor.moveToFirst();
            }
        	else { System.out.println("Cursor  vide");}
            return mCursor;
        }
     
        //---updates a title---
        public boolean updateAliment(long rowId, String t, byte[] image, int dl, int ty) 
        {
            ContentValues args = new ContentValues();
            args.put(KEY_TITRE, t);
            args.put(KEY_IMAGE, image);
            args.put(KEY_DANSLIST, dl);
            args.put(KEY_TYPE, ty);
            return db.update(DATABASE_TABLE, args, 
                             KEY_ROWID + "=" + rowId, null) > 0;
        }
     
        public long insertAliment(UnAliment al)
    	{
    		//Création d'un ContentValues (fonctionne comme une HashMap)
    		ContentValues values = new ContentValues();
     
    		//on lui ajoute une valeur associé à une clé (qui est le nom de la colonne dans laquelle on veut mettre la valeur)
    		values.put(KEY_TITRE, al.getTitre());
    		values.put(KEY_IMAGE, al.getImage());
    		values.put(KEY_DANSLIST, al.getDansList());
    		values.put(KEY_TYPE, al.getType());
     
    		System.out.println("Aliment inseré");
    		//on insère l'objet dans la BDD via le ContentValues
    		return db.insert(DATABASE_TABLE, null, values);
    	}
     
    	public int updateAliment(long l, UnAliment al)
    	{
    		//La mise à jour d'un livre dans la BDD fonctionne plus ou moins comme une insertion
    		//il faut simple préciser quelle livre on doit mettre à jour grâce à l'ID
    		ContentValues values = new ContentValues();
    		values.put(KEY_TITRE, al.getTitre());
    		values.put(KEY_IMAGE, al.getImage());
    		values.put(KEY_DANSLIST, al.getDansList());
    		values.put(KEY_TYPE, al.getType());
    		return db.update(DATABASE_TABLE, values, KEY_ROWID + " = " +l, null);
    	}
     
    	public int removeAlimentWithID(long l)
    	{
    		//Suppression d'un livre de la BDD grâce à l'ID
    		return db.delete(DATABASE_TABLE, KEY_ROWID + " = " +l, null);
    	}
     
    	public UnAliment getAlimentWithTitre(String titre)
    	{
    		//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
    		Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_TITRE + " LIKE \"" + titre +"\"", null, null, null, null);
    		return cursorToAliment(c);
    	}
     
    	public UnAliment getAlimentWithId(int id)
    	{
    		//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
    		Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_ROWID + " LIKE \"" + id +"\"", null, null, null, null);
    		return cursorToAliment(c);
    	}
     
    	public UnAliment getAlimentWithDansList(int dl)
    	{
    		//Récupère dans un Cursor les valeur correspondant à un livre contenu dans la BDD (ici on sélectionne le livre grâce à son titre)
    		Cursor c = db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITRE, KEY_IMAGE, KEY_DANSLIST, KEY_TYPE}, KEY_DANSLIST + " = \"" + dl +"\"", null, null, null, null);
    		return cursorToAliment(c);
    	}
     
    	//Cette méthode permet de convertir un cursor en un livre
    	private UnAliment cursorToAliment(Cursor c)
    	{
    		//si aucun élément n'a été retourné dans la requête, on renvoie null
    		if (c.getCount() == 0)
    			return null;
     
    		//Sinon on se place sur le premier élément
    		c.moveToFirst();
     
    		//On créé un livre
    		UnAliment al = new UnAliment();
     
    		//on lui affecte toutes les infos grâce aux infos contenues dans le Cursor
    		al.setIdAl(c.getInt(NUM_KEY));
    		al.setTitre(c.getString(NUM_TITRE));
    		al.setImage(c.getBlob(NUM_IMAGE));
    		al.setDansList(c.getInt(NUM_DANSLIST));
    		al.setType(c.getInt(NUM_TYPE));
     
    		//On ferme le cursor
    		c.close();
     
    		//On retourne le livre
    		return al;
    	}
     
    }

    Merci de m'aider, si vous voulez d'autres infos, demandez moi !!
    J'ai vraiment besoin de la réponse !!

  5. #5
    Membre éclairé

    Homme Profil pro
    Développeur mobile iOS / Android
    Inscrit en
    Décembre 2008
    Messages
    259
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur mobile iOS / Android
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 259
    Points : 690
    Points
    690
    Par défaut
    Tu as quoi comme message d'erreur?
    « Il est assez difficile de trouver une erreur dans son code quand on la cherche. C’est encore bien plus dur quand on est convaincu que le code est juste. » - Steve McConnell

    N'oubliez pas de consulter les FAQ Swift, Android
    Tutoriel : Développer une application multilingue sous iOS

  6. #6
    Membre du Club
    Homme Profil pro
    Developpeur Android
    Inscrit en
    Juin 2012
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur Android
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 65
    Points : 57
    Points
    57
    Par défaut
    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
    06-25 10:11:21.656: W/dalvikvm(1059): Exception Ljava/lang/NullPointerException; thrown during Lcom/example/medichome/DBAdapterAliment;.<clinit>
    06-25 10:11:21.666: D/AndroidRuntime(1059): Shutting down VM
    06-25 10:11:21.666: W/dalvikvm(1059): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    06-25 10:11:21.666: E/AndroidRuntime(1059): Uncaught handler: thread main exiting due to uncaught exception
    06-25 10:11:21.676: E/AndroidRuntime(1059): java.lang.ExceptionInInitializerError
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.example.medichome.CoursesListe.onCreate(CoursesListe.java:30)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.os.Looper.loop(Looper.java:123)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at android.app.ActivityThread.main(ActivityThread.java:4363)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at java.lang.reflect.Method.invokeNative(Native Method)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at java.lang.reflect.Method.invoke(Method.java:521)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at dalvik.system.NativeStart.main(Native Method)
    06-25 10:11:21.676: E/AndroidRuntime(1059): Caused by: java.lang.NullPointerException
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	at com.example.medichome.DBAdapterAliment.<clinit>(DBAdapterAliment.java:55)
    06-25 10:11:21.676: E/AndroidRuntime(1059): 	... 14 more
    06-25 10:11:21.706: I/dalvikvm(1059): threadid=7: reacting to signal 3
    06-25 10:11:21.706: E/dalvikvm(1059): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
    06-25 10:11:23.099: I/Process(1059): Sending signal. PID: 1059 SIG: 9
    Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

    J'ai copié collé cet adapter d'un de mes adapter qui fonctionne très bien.
    La seule différence, c'est que dans celui qui ne marche pas, la variable context est déclaré en static.
    Je l'ai déclaré en static, sinon je ne peut faire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public static final Bitmap image1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.pomme);
    Je ne sais pas si je fait fausse route ou pas, comment régler ce problème....
    Please help me !

  7. #7
    Membre du Club
    Homme Profil pro
    Developpeur Android
    Inscrit en
    Juin 2012
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Developpeur Android
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2012
    Messages : 65
    Points : 57
    Points
    57
    Par défaut
    Ce n'est apparemment pas ça.
    J'ai retirer l'attribut image pour pouvoir passer la variable context hors static et le problème persiste.

    Je suis perdu....

  8. #8
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Si si , c'est bien ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    com.example.medichome.DBAdapterAliment.<clinit>
    veut dire dans le code d'initialisation de la classe (même pas de l'instance, donc initialisation des statiques).
    Ce qui correspond bien à:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public static final Bitmap image1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.pomme);
    Dans ce code là, la seule chose qui puisse envoyer un NPE est "context.getResources()"... donc "context" est null...

    Forcément il est null puisqu'il n'est pas initialisé (et d'ailleurs ne peut pas l'être), dès qu'on y fait une référence extérieure pour l'initialiser, la classe doit être construite (et on passe dans le code ci-dessus => context y est donc toujours null).

    De toute manière, stocker des bitmap, comme cela, dans une classe qui n'a à priori aucun rapport avec l'UI est un peu cavalier... d'autant que seul le "int" de la ressource suffit à priori...
    (et il vaut mieux utiliser setImageResource() que setImageBitmap() sur les ImageView).
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Erreur SQLite Android
    Par mokhtar1105 dans le forum Android
    Réponses: 4
    Dernier message: 29/02/2012, 18h57
  2. SQL -> Sqlite android
    Par taz007_dj dans le forum Android
    Réponses: 0
    Dernier message: 10/10/2011, 22h07
  3. Compatibilité SQLite android
    Par matchlesspseudo dans le forum Windev Mobile
    Réponses: 21
    Dernier message: 01/07/2011, 10h14
  4. sqlite android ( affichage des données stockés)
    Par nagca dans le forum Android
    Réponses: 5
    Dernier message: 26/04/2011, 13h21
  5. Aide SQLite Android
    Par Zaark dans le forum Android
    Réponses: 0
    Dernier message: 13/01/2011, 16h56

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo