我创建了这个表:
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement not null, "
+ KEY_TITLE + " text not null, "
+ KEY_BODY + " text not null, " + KEY_DATE_TIME + " text not null);"
以下是我插入的内容:
ContentValues initialValues = new ContentValues();
initialValues.put("title", title);
initialValues.put("body", body);
initialValues.put("abc", anc);
return mDb.insert(DATABASE_TABLE, null, initialValues);
但由于某些错误,我的应用程序强制关闭。请帮帮我......
Logcat错误:
java.lang.NullPointerException
com.maddy.task_reminder.ReminderDbAdapter.createReminder(ReminderDbAdapter.java:133)
06-09 23:28:44.112: E/AndroidRuntime(412): at com.maddy.task_reminder.edit_activity$4.onClick(edit_activity.java:143)
答案 0 :(得分:0)
如果你看一下
java.lang.NullPointerException
com.maddy.task_reminder.ReminderDbAdapter.createReminder(ReminderDbAdapter.java:133)
06-09 23:28:44.112: E/AndroidRuntime(412): at com.maddy.task_reminder.edit_activity$4.onClick(edit_activity.java:143)
您在edit_activity.java:143
和ReminderDbAdapter.java:133
时遇到错误。那里有什么?
我的代码看起来像这个
`public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_REL + " TEXT NOT NULL);"
);
}`
和
public long createEntry(String name, String rel) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_REL, rel);
return water.insert(DATABASE_TABLE, null, cv);
}
它工作正常。
答案 1 :(得分:0)
如果您正在关注网络上的通用DBAdapter示例,请检查您是否致电:
mDbHelper.open();
在你的活动中。否则,mDb
永远不会在数据库帮助程序类中实例化,它始终是null
。
来自评论的补充:
这是您的open()
方法。
public ReminderDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
如果你的ReminderDbAdapter构造函数看起来不像这样:
public ReminderDbAdapter(Context context) {
...
mCtx = context;
open(); // Do you have this line?
}
然后你需要在你的活动中明确地调用ReminderDbAdapter.open()
,可能是这样的:
ReminderDbAdapter mDbHelper = new ReminderDbAdapter(this);
mDbHelper.open();