无法在sqlite数据库中插入数据?

时间:2014-05-04 05:49:07

标签: android sqlite

嗨我刚接触到android并且正在创建一个警报我无法在数据库中插入小时和分钟这里下面是我的所有源代码任何一个请帮我解决这个问题。在运行程序时,我在插入方法时得到Nullpointer异常。

public  void setListAdapter(SimpleCursorAdapter mAdapter) {
    String[] alarm = new String[] { "select*from " + DATABASE_TABLE_NAME };
    return ;
}

private void setAlarm(Calendar targetCal) {

    intent = new Intent(getBaseContext(), AlarmReciever.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1,
            intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
            pendingIntent);
    textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "
            + targetCal.getTime() + "\n" + "Repeat every 5 minutes\n"
            + "***\n");
    int hour = targetCal.get(Calendar.HOUR_OF_DAY);
    int minute = targetCal.get(Calendar.MINUTE);
    Log.d(TAG, "value" + hour + minute);

    String h = Integer.toString(hour);
    String m = Integer.toString(minute);
    ContentValues values = new ContentValues();
    values.put("hour", h);
    values.put("minute", m);
    insertalarmclock(h, m);
    setListAdapter(mAdapter);
    // private void clearAll() {
    // SQLiteDatabase mDB = null;
    // mDB.delete(MyDatabaseHelper.DATABASE_TABLE_NAME, null, null);
    //
    // }
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXIST" + DATABASE_TABLE_NAME + ""
            + KEY_ID + "INTEGER AUTOINCRREMENT ," + KEY_ALARM_HOUR
            + "TIME," + KEY_ALARM_MINUTE + "TIME,)");
}

public long insertalarmclock(String h, String m) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("hour", h);
    values.put("minute", m);
    String val = (String) values.get(h);
    Log.d(TAG, "value" + val + h + m);

    return mDB.insert(DATABASE_TABLE_NAME, null, values);

}

private SQLiteDatabase getWritableDatabase() {
    return null;
}

我的错误是         显示java.lang.NullPointerException         在insertalarmclock(AlarmActivity.java:435)         在setAlarm(AlarmActivity.java:412)

1 个答案:

答案 0 :(得分:0)

insertalarmclock()中,使用insert()致电您db的{​​{1}},而不是getWritableDatabase() mDb