mardi 30 juin 2015

Android app crash sqlite

I built database handler and avery time im calling function that return a number of rows the app crashes This is the code:

    private static final String CREATE_TABLE_USERS = "CREATE TABLE " + TABLE_NAME + "("
        + COLUMN_NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COLUMN_NAME_USERNAME + " TEXT, "
        + COLUMN_NAME_PASSWORD + " TEXT, "
        + COLUMN_NAME_EMAIL + " TEXT " + ")";

public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_USERS);
}

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

public int getCountRows(){
    SQLiteDatabase db = this.getWritableDatabase();
    int i = db.rawQuery("SELECT * FROM " + TABLE_NAME, null).getCount();
    return i;
}

The usage:

    public void loginButtonClicked(View view){
    dbHandler.createUser(user);
    Toast.makeText(this, dbHandler.getCountRows(), Toast.LENGTH_SHORT).show();
}

Thanks for help

this is the logcat a lot of not understandable errors bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

logcat
  **java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.company.loginregistersystem/com.example.company.loginregistersystem.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT  * FROM users
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at **android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at** android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.**app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCall**er.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT  * FROM users
            at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
            at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
            at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
            at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
            at com.example.company.loginregistersystem.DBHandler.getCountRows(DBHandler.java:62)
            at com.example.company.loginregistersystem.MainActivity.onCreate(MainActivity.java:28)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)**

Aucun commentaire:

Enregistrer un commentaire