遵循Android标签的任何人都可能对我很熟悉.我很难为自己的高分实现sqlite数据库.这也是我第一次使用sqlite.
我正在尝试仅插入两个变量-int和long.我的insert()方法无法正常工作.除了上面讨论的内容外,任何其他建议也应受到赞赏.
预先感谢您的帮助.
Highscores.java
public class Highscores extends Activity { DatabaseHelper dh; sqliteDatabase db; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.highscoresmain); dh = new DatabaseHelper(this); dh.openDB(db); long x = 11; int y = 22; TextVIEw rank = (TextVIEw)findVIEwByID(R.ID.rank); TextVIEw percentage = (TextVIEw)findVIEwByID(R.ID.percentage); TextVIEw score = (TextVIEw)findVIEwByID(R.ID.score); TextVIEw r1r = (TextVIEw)findVIEwByID(R.ID.r1r); TextVIEw r1p = (TextVIEw)findVIEwByID(R.ID.r1p); TextVIEw r1s = (TextVIEw)findVIEwByID(R.ID.r1s); dh.insert(x, y, db); //line 55 scores = dh.getscore(db); percentages = dh.getPercentage(db); rank.setText("Rank Column - TEST"); percentage.setText("Percentage Column - TEST "); score.setText("score Column - Test"); r1r.setText("test..rank"); r1p.setText("" + percentages); r1s.setText("test..score"); table = (tableLayout)findVIEwByID(R.ID.tableLayout); dh.closeDB(db); }}
DatabaseHelper.java
public class DatabaseHelper extends sqliteOpenHelper { sqliteDatabase db; // table columns names. private static final String RANK = "_ID"; private static final String score = "score"; private static final String PERCENTAGE = "percentage"; public DatabaseHelper(Context context) { super(context, DB_name, null, DATABASE_VERSION); } public sqliteDatabase openDB(sqliteDatabase db) { db = this.getWritableDatabase(); return db; } public int getPercentage(sqliteDatabase db) { //Cursor c = db.rawquery("SELECT " + PERCENTAGE + " FROM " + table + " WHERE " + PERCENTAGE + " = " + 22 + ";", null); Cursor c = db.rawquery("SELECT " + PERCENTAGE + " FROM " + table + ";", null); int i = 0; if(c.getCount() == 0) { i = 333; } else if (c.getCount() == 1) { i = 444; } else { i = 888; /*c.movetoFirst(); int columnIndex = c.getInt(c.getColumnIndex("PERCENTAGE")); if(columnIndex != -1) { i = c.getInt(columnIndex); } else { i = 999; }*/ } c.close(); return i; } //Insert new record. public long insert(long score, int percentage, sqliteDatabase db) { ContentValues values = new ContentValues(); values.put(score, score); values.put(PERCENTAGE, percentage); return db.insert(table, null, values); //line 120 } public synchronized voID closeDB(sqliteDatabase db) { if(db != null) { db.close(); } super.close(); } public voID onCreate(sqliteDatabase db) { db.execsql("CREATE table " + table + " (" + RANK + " INTEGER PRIMARY KEY autoINCREMENT," + score + " LONG," + PERCENTAGE + " INTEGER" + ");");}
LogCat输出
01-03 17:09:18.232: E/AndroIDRuntime(1712): FATAL EXCEPTION: main01-03 17:09:18.232: E/AndroIDRuntime(1712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Highscores}: java.lang.NullPointerException01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2180)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread.access0(ActivityThread.java:141)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.os.Handler.dispatchMessage(Handler.java:99)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.os.Looper.loop(Looper.java:137)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread.main(ActivityThread.java:5039)01-03 17:09:18.232: E/AndroIDRuntime(1712): at java.lang.reflect.Method.invokeNative(Native Method)01-03 17:09:18.232: E/AndroIDRuntime(1712): at java.lang.reflect.Method.invoke(Method.java:511)01-03 17:09:18.232: E/AndroIDRuntime(1712): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)01-03 17:09:18.232: E/AndroIDRuntime(1712): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:560)01-03 17:09:18.232: E/AndroIDRuntime(1712): at dalvik.system.NativeStart.main(Native Method)01-03 17:09:18.232: E/AndroIDRuntime(1712): Caused by: java.lang.NullPointerException01-03 17:09:18.232: E/AndroIDRuntime(1712): at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:120)01-03 17:09:18.232: E/AndroIDRuntime(1712): at com.example.test.Highscores.onCreate(Highscores.java:55)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.Activity.performCreate(Activity.java:5104)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)01-03 17:09:18.232: E/AndroIDRuntime(1712): at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2144)01-03 17:09:18.232: E/AndroIDRuntime(1712): ... 11 more
解决方法:
在这行上,打开数据库:
dh.openDB(db);
这会将数据库的成员变量存储在DatabaseHelper.db中(但没有其他地方,尤其是在Highscores.db中).然后,您致电:
dh.insert(x, y, db);
使用Highscores中的空db对象.最后,在insert()中,使用此空db对象:
return db.insert(table, null, values);
相反,您应该使用以下代码作为insert()方法:
public long insert(long score, int percentage) { // Remove db parameter ContentValues values = new ContentValues(); values.put(score, score); values.put(PERCENTAGE, percentage); return db.insert(table, null, values); // This will use the member variable}
然后,调用dh.insert(x,y);代替.
您还应该将openDB(sqliteDatabase db)更改为openDB(),以便它可以写入正确的数据库.就目前而言,它只会覆盖局部变量的值(一旦函数作用域完成,它就不会改变).
总结以上是内存溢出为你收集整理的尝试插入SQLite时出现NullPointerException-Android全部内容,希望文章能够帮你解决尝试插入SQLite时出现NullPointerException-Android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)