应用程序崩溃数据库更新

时间:2014-05-25 14:35:14

标签: android database sqlite crash

我创建了这个数据库但是当我尝试将数据添加到数据库时,我的应用程序停止了工作。 (崩溃)

当我使用addSigarette或addUser方法时会发生这种情况。

代码:

package com.amaze.quit.app;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHandler extends SQLiteOpenHelper {



// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "QuitSmokeDatabase";

//  table names
private static final String TABLE_USER = "users";
private static final String TABLE_SIGARETTEN = "sigaretten";


// User Table Columns names
private static final String USER_UID = "uID";
private static final String USER_SID = "sID";
private static final String USER_PERDAG = "per_dag";
private static final String USER_LEVEL = "level";


// Sigaretten Table Columns names
private static final String SIGARETTEN_SID = "sID";
private static final String SIGARETTEN_MERK = "merk";
private static final String SIGARETTEN_AANTAL = "aantal";
private static final String SIGARETTEN_TEER = "teer";
private static final String SIGARETTEN_NICOTINE = "nicotine";
private static final String SIGARETTEN_PRIJS = "prijs";



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


/* creating Tables */
@Override
public void onCreate(SQLiteDatabase db) {
    /* User table */
    String CREATE_USER_TABLE = "CREATE TABLE "
            + TABLE_USER + "("
            + USER_UID + " INTEGER PRIMARY KEY,"
            + USER_SID + " INTEGER,"
            + USER_PERDAG + " INTEGER,"
            + USER_LEVEL + " INTEGER" + ")";
    db.execSQL(CREATE_USER_TABLE);

      /* Sigaretten table */
    String CREATE_SIGARETTEN_TABLE = "CREATE TABLE "
            + TABLE_SIGARETTEN + "("
            + SIGARETTEN_SID + " INTEGER PRIMARY KEY,"
            + SIGARETTEN_MERK + " TEXT,"
            + SIGARETTEN_AANTAL + " INTEGER,"
            + SIGARETTEN_TEER + " REAL"
            + SIGARETTEN_NICOTINE + " REAL"
            + SIGARETTEN_PRIJS + " REAL"
            + ")";
    db.execSQL(CREATE_SIGARETTEN_TABLE);



}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
    /*Drop older table if existed*/
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SIGARETTEN);

    /* Create tables again*/
    onCreate(db);
}

/* Adding new User*/
public void addUser(User user) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(USER_UID, user.getuID()); // user id
    values.put(USER_SID, user.getsID()); // user sigaretten id
    values.put(USER_PERDAG, user.getPerDag()); // hoeveel die smoked per dag
    values.put(USER_LEVEL, user.getLevel()); // level van user. bij nieuwe user gewoon 1.

    // Inserting Row
    assert db != null;
    db.insert(TABLE_USER, null, values);
    db.close(); // Closing database connection
}

public void addSigarette(Sigaretten sigaret) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(SIGARETTEN_SID, sigaret.getsID());
    values.put(SIGARETTEN_MERK, sigaret.getMerk());
    values.put(SIGARETTEN_AANTAL, sigaret.getAantal());
    values.put(SIGARETTEN_TEER, sigaret.getTeer());
    values.put(SIGARETTEN_NICOTINE, sigaret.getNicotine());
    values.put(SIGARETTEN_PRIJS, sigaret.getPrijs());
    // Inserting Row
    assert db != null;
    db.insert(TABLE_SIGARETTEN, null, values);
    db.close(); // Closing database connection
}













/* Getting single User*/
public User getUser(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    assert db != null;
    Cursor cursor = db.query(TABLE_USER, new String[] {
                    USER_UID,
                    USER_SID,
                    USER_PERDAG,
                    USER_LEVEL }, USER_UID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    User user = new User(Integer.parseInt(cursor.getString(0)),
            Integer.parseInt(cursor.getString(1)),
            Integer.parseInt(cursor.getString(2)),
            Integer.parseInt(cursor.getString(3)));
    /* return contact */
    return user;
}

/* Getting single Sigarette*/
public Sigaretten getSigaret(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    assert db != null;
    Cursor cursor = db.query(TABLE_SIGARETTEN, new String[] {
                    SIGARETTEN_SID ,
                    SIGARETTEN_MERK,
                    SIGARETTEN_AANTAL,
                    SIGARETTEN_TEER,
                    SIGARETTEN_NICOTINE,
                    SIGARETTEN_PRIJS}, SIGARETTEN_SID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Sigaretten sigaret = new Sigaretten(
            Integer.parseInt(cursor.getString(0)),
            Float.parseFloat(cursor.getString(5)),
            cursor.getString(2),
            Integer.parseInt(cursor.getString(3)),
            Float.parseFloat(cursor.getString(4)),
            Float.parseFloat(cursor.getString(5)));
    /* return sigaret */
    return sigaret;
}










}

堆栈跟踪:

05-25 16:48:29.039  15123-15123/com.amaze.quit.app W/System.err﹕ java.lang.NullPointerException
05-25 16:48:29.039  15123-15123/com.amaze.quit.app W/System.err﹕ at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-25 16:48:29.039  15123-15123/com.amaze.quit.app W/System.err﹕ at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-25 16:48:29.039  15123-15123/com.amaze.quit.app W/System.err﹕ at  com.amaze.quit.app.DatabaseHandler.addSigarette(DatabaseHandler.java:172)
05-25 16:48:29.039  15123-15123/com.amaze.quit.app W/System.err﹕ at com.amaze.quit.app.SetupBrandAmount$1.onClick(SetupBrandAmount.java:115)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.view.View.performClick(View.java:4438)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.view.View$PerformClick.run(View.java:18422)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-25 16:48:29.049  15123-15123/com.amaze.quit.app W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

你在这里遗漏了几个逗号:

+ SIGARETTEN_TEER + " REAL,"
+ SIGARETTEN_NICOTINE + " REAL,"

修复onCreate()后,卸载您的代码,以便删除可能的旧数据库文件。如果您仍然遇到问题,请在问题中包含异常堆栈跟踪。

getDatabaseLocked()中的NPE:您已将空Context传递给您的帮助构造函数。