0

我已经修剪了代码以尝试仅包含所需的片段。我正在制作一个琐事游戏,当游戏完成时,Results.java 会显示当前游戏的统计数据,并将完成的游戏发送到高分(SQLite 数据库)。当调用 Results.java 类时,它会给出一个空指针异常(我在 NP 结果的位置进行了评论)。

这是我第一次尝试在我的项目中创建和实施 SQLite 数据库。非常感谢任何其他关于代码结构或我的数据库代码看起来有问题的其他建议!

数据库助手.java

public class DatabaseHelper extends SQLiteOpenHelper { 

    //variables

    public DatabaseHelper(Context context) { 
        super(context, DB_NAME, null, DATABASE_VERSION); 
        myDB = getWritableDatabase();
    }

    public void createDatabase() throws IOException {
        boolean dbExist = checkDatabase();
        if(dbExist) {
            //Do nothing - DB already exists.
        } else {
            myDB.execSQL("CREATE TABLE " + TABLE + " ("
                            + RANK + " INTEGER,"
                            + SCORE + " LONG,"
                            + PERCENTAGE + " INTEGER"
                            + ");");
        }
    }

    //Insert new record.
    public long insert(int rank, long score, int percentage) {
        ContentValues values = new ContentValues();
        values.put(RANK, rank);
        values.put(SCORE, score);
        values.put(PERCENTAGE, percentage);

        return myDB.insert(DB_NAME, null, values);  //Line 56
    }

                //other methods...

    public void onCreate(SQLiteDatabase db) {}
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

结果.java

public class Results extends Activity {

    DatabaseHelper dh;

    int rank;
    long score;

    @Override
    public void onCreate(Bundle savedInstanceState) {

                    //...
                    dh = new DatabaseHelper(this);
                    score = getIntent().getLongExtra("score", -1);

                    //...
    }

    public void showResults() {

        //...

        percentage = 10 * cAnswers;

                          //...

        dh.insert(1, score, percentage);  //Line 100
    }

    public void restart() {
        //...
    }
}

编辑: NullPointer 错误消失了,但正在显示一个新的 LogCat 输出,可以在下面找到。

LogCat 输出

12-18 19:47:48.280: E/SQLiteLog(1527): (1) no such table: highscoresDatabase
12-18 19:47:48.370: E/SQLiteDatabase(1527): Error inserting percentageX=30 scoreX=827 rankX=1
12-18 19:47:48.370: E/SQLiteDatabase(1527): android.database.sqlite.SQLiteException: no such table: highscoresDatabase (code 1): , while compiling: INSERT INTO highscoresDatabase(percentageX,scoreX,rankX) VALUES (?,?,?)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:56)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.Results.showResults(Results.java:100)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.Results.onCreate(Results.java:50)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.Activity.performCreate(Activity.java:5008)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.os.Looper.loop(Looper.java:137)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

4

你不能放

DatabaseHelper dh = new DatabaseHelper(this);

在字段声明空间中。this这是因为您至少直到onCreate或其他 Activity 方法都无法访问上下文。尝试移动引用thisinside的行onCreate

于 2012-12-26T18:18:47.623 回答
4

您只能在onCreate()调用后使用您的 Activity 的上下文,否则它还无效。只需将您的代码更改为:

DatabaseHelper db;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    db = new DatabaseHelper(this);

添加

no such table: highscoresDatabase

您使用了错误的字符串,使用TABLE而不是DB_NAME第 56 行:

return myDB.insert(TABLE, null, values); // Line 56
于 2012-12-26T18:18:53.740 回答