jeudi 9 juillet 2015

SQLite - app ran the first time and now it wont start and is stuck on black screen even after reinstall(or rerun). (pics/code included)Android

Hi ive been learning SQLite, trying to save strings in a table. The first time I ran the app it worked and i think i was able to add things to the database, but then after i closed it and restarted the app it goes to a white screen for 4 sec and then a blackscreen while logcat spits out the same GC_FOR_ALLOC freed line over and over

heres what logcat looks like, any idea what this means? http://ift.tt/1ILn0uh

Also here is my SQLite code

in MainActivity

public static DBHandler dbHandler;

in MainActiviy's onCreate

dbHandler = new DBHandler(this, null, null, 1);   
int count = dbHandler.getRowCount();
    for(int i = 0; i< count; i++){
        GridItem gi = new GridItem();
        gi.setUserCreated(true);
        gi.setSong(dbHandler.getSongName(i));
        gi.setSongId(dbHandler.getSongPath(i));
        gi.setIcon(R.drawable.onion1);
        items.add(gi);
    }

DBhandler class

public class DBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "songs.db";
public static final String TABLE_SONGS = "Songs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_SONG_NAME ="_songName";
public static final String COLUMN_SONG_PATH="_songPath";

public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " +TABLE_SONGS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_SONG_NAME+ " TEXT, "+
            COLUMN_SONG_PATH+" TEXT);";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_SONGS);
    onCreate(db);
}

public void addGridItem(GridItem gridItem){
    ContentValues values = new ContentValues();
    values.put(COLUMN_SONG_NAME, gridItem.getSong());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_SONGS, null, values);
    db.close();
}

public int getRowCount(){
    String countQuery = "SELECT * FROM " + TABLE_SONGS;
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int cnt = cursor.getCount();
    cursor.close();
    return cnt;
}

public void deleteGridItem(int rowId){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_SONGS + " WHERE " + COLUMN_ID + "=\"" + rowId + "\";");
    db.close();
}

public String getSongPath(int rowNumber){
    String path;
    String rowNum = rowNumber+"";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM "+TABLE_SONGS+" WHERE 1";

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();


    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex(COLUMN_ID)).equals(rowNum)){
            path = c.getString(c.getColumnIndex(COLUMN_SONG_PATH));
            db.close();
            return path;
        }
    }
    db.close();
    return null;
}

public String getSongName(int rowNumber){
    String path;
    String rowNum = rowNumber+"";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM "+TABLE_SONGS+" WHERE 1";

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex(COLUMN_ID)).equals(rowNum)){
            path = c.getString(c.getColumnIndex(COLUMN_SONG_NAME));
            db.close();
            return path;
        }
    }
    db.close();
    return null;
}
}

Aucun commentaire:

Enregistrer un commentaire