gpt4 book ai didi

android - 为什么我的数据库使应用程序强制关闭但在我清除数据后工作正常?

转载 作者:行者123 更新时间:2023-11-30 03:50:55 25 4
gpt4 key购买 nike

如果我清除我的应用数据然后运行它,它工作正常。如果我重新打开或重新运行我的应用程序,它会强制关闭并抛出 java.lang.NullPointerException?错误在这一段代码中

private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TBL + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_LEVEL + " INTEGER NOT NULL, " +
KEY_HEALTH + " INTEGER NOT NULL, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_CRIT + " INTEGER NOT NULL, " +
KEY_CRIT_RANGE + " INTEGER NOT NULL, " +
KEY_CRIT_INC + " INTEGER NOT NULL, " +
KEY_HIT_RANGE + " INTEGER NOT NULL, " +
KEY_HIT_INC + " INTEGER NOT NULL, " +
KEY_CHAR_IMG + " INTEGER NOT NULL, " +
KEY_TOTAL_XP + " INTEGER NOT NULL, " +
KEY_XP + " INTEGER NOT NULL, " +
KEY_XP_NEEDED + " INTEGER NOT NULL, " +
KEY_COINS + " INTEGER NOT NULL, " +
KEY_SMALL_POTS + " INTEGER NOT NULL, " +
KEY_LARGE_POTS + " INTEGER NOT NULL)"
);

values = new ContentValues();
values.put(KEY_NAME, "User1");
values.put( KEY_LEVEL, 1);
values.put(KEY_HEALTH, 10);
values.put(KEY_CRIT, 5);
values.put(KEY_CRIT_RANGE, 2);
values.put(KEY_CRIT_INC, 2);
values.put(KEY_HIT_RANGE, 2);
values.put(KEY_HIT_INC, 0);
values.put(KEY_TOTAL_XP, 0);
values.put(KEY_XP, 0);
values.put(KEY_XP_NEEDED, 5);
values.put(KEY_COINS, 5);
values.put(KEY_SMALL_POTS, 0);
values.put(KEY_LARGE_POTS, 0);
values.put(KEY_CHAR_IMG, 1);
db.insert(DB_TBL, null, values);

}

java.lang.NullPointerException 指向这个

  public int getLevel() {
open();
int level = values.getAsInteger(KEY_LEVEL);
close();
return level;
}

这是我的堆栈跟踪

01-04 14:45:45.694: W/dalvikvm(20858): threadid=9: thread exiting with uncaught exception (group=0x4001e578)
01-04 14:45:45.694: E/AndroidRuntime(20858): FATAL EXCEPTION: UpdateThread
01-04 14:45:45.694: E/AndroidRuntime(20858): java.lang.NullPointerException
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.SQLiteTable.getLevel(SQLiteTable.java:110)
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.Profile.<init>(Profile.java:57)
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.MainMenuScene.onMenuItemClicked(MainMenuScene.java:37)

这是open()close()

public static void open() throws SQLException{
ourDatabase = ourHelper.getWritableDatabase();
}

public static void close() throws SQLException{
ourHelper.close();
}

最佳答案

由于在至少运行一次应用程序后创建新的 DbHelper 对象时不会调用 onCreate(),因此 values 为 null。

如果您想从表中提取此数据,请使用 SQLiteDatabase#query() .

public int getLevel() {
int level = -1;
open();

Cursor cursor = ourDatabase.query(DB_TBL, new String[] {KEY_LEVEL}, null, null, null, null, null);
if(cursor.moveToFirst())
level = cursor.getInt(0); // get the first column from your Select statement, i.e. KEY_LEVEL

cursor.close();
close();
return level;
}

但是这只会返回找到的第一个“级别”,使用 _id 参数来选择特定的行。

关于android - 为什么我的数据库使应用程序强制关闭但在我清除数据后工作正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14165113/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com