因此,我设法使用整个互联网上的教程创建数据库处理程序。
这是插入操作的片段
public ChosenParkingSlot createSlot(String slot) {
ContentValues values = new ContentValues();
values.put(ChosenSlotDatabaseHandler.CHOSEN_SLOT_NAME, slot);
long insertId = database.insert(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT, null,
values);
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
allColumns, ChosenSlotDatabaseHandler.CHOSEN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
ChosenParkingSlot newSlot = cursorToSlot(cursor);
cursor.close();
return newSlot;
}
allColumns
包含:
private String[] allColumns = {
ChosenSlotDatabaseHandler.CHOSEN_ID,
ChosenSlotDatabaseHandler.CHOSEN_SLOT_NAME };
以下是我在课堂上称呼它的方式
ChosenSlotDAO chosenSlotDAO = new ChosenSlotDAO(this);
chosenSlotDAO.createSlot(slotA);
TextView arriveA = (TextView)findViewById(R.id.arriveView);
arriveA.setText(chosenSlotDAO.getSlot());
所以基本上我想从名为chosen_parking_slot的两列表中通过名为arriA的TextView
显示单个值。但是我收到了错误
06-11 12:57:32.939: E/AndroidRuntime(10108): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.km.parkit/com.km.parkit.ParkAArrive}: java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
我在这里缺少什么?
答案 0 :(得分:1)
您应首先初始化database
对象,然后才能插入或执行您想要的操作。我认为现在你只有它的声明(SQLiteDatabase database;
),而不是像SQLiteDatabase database = mDbHelper.getWritableDatabase();
这样的整个初始化。更多信息 - > http://developer.android.com/training/basics/data-storage/databases.html