空指针异常SQLite报警数据库

时间:2014-07-01 15:41:47

标签: java android sqlite

嘿devs我是Android开发的新手我面临一个问题我正在创建一个警报应用程序我自己插入数据库的时间,我希望警报在那时触发。

这是我的MainActivity代码

public class MainActivity extends Activity {
    final String PREF_NAME = "Preferences";
    Context context;
    static PendingIntent pendingIntent;
    static AlarmManager alarmManager;
    Switch switch1;
    Intent intentAlarm;

    DatabaseHandler dhb = new DatabaseHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Calendar cal = Calendar.getInstance();

        intentAlarm = new Intent(context, AlarmReciver.class);

        // setting Time!
        Log.d("Insert: ", "Inserting...");
        dhb.createAlarm(new AlarmModel(1, 1, 21, 5));

        context = MainActivity.this;

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        switch1 = (Switch) findViewById(R.id.tgButton);

        switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                if (seherBtn.isChecked()) {

                    List<AlarmModel> alarms = dhb.getAlarms();

                    for (AlarmModel al : alarms) {
                        pendingIntent = PendingIntent.getBroadcast(context,
                                al.getId(), intentAlarm,
                                PendingIntent.FLAG_UPDATE_CURRENT);
                        cal.set(Calendar.MONTH, 6);
                        cal.set(Calendar.YEAR, 2014);
                        cal.set(Calendar.DAY_OF_MONTH, al.getDay());
                        cal.set(Calendar.HOUR_OF_DAY, al.getTimeHour());
                        cal.set(Calendar.MINUTE, al.getTimeMinute());
                        cal.set(Calendar.SECOND, 0);

                        alarmManager.set(AlarmManager.RTC_WAKEUP,
                                cal.getTimeInMillis(), pendingIntent);
                    }


                } else {
                    alarmManager.cancel(pendingIntent);


                }
            }

        });

    }

我的DatabaseHelper类 如果我在某个地方出错了,请帮助我,请告诉我可能的解决方案 谢谢......

public class DatabaseHandler extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "alarmClock.db";

    public static final String TABLE_NAME = "alarm";
    public static final String COLUMN_NAME_ALARM_NAME = "name";
    public static final String COLUMN_NAME_ALARM_TIME_HOUR = "hour";
    public static final String COLUMN_NAME_ALARM_TIME_MINUTE = "minute";
    public static final String COLUMN_NAME_ALARM_DAYS = "days";
    public static final String ID = "id";

    private static final String SQL_CREATE_ALARM = "CREATE TABLE " + TABLE_NAME
            + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME_ALARM_NAME + " TEXT," + COLUMN_NAME_ALARM_TIME_HOUR
            + " INTEGER," + COLUMN_NAME_ALARM_TIME_MINUTE + " INTEGER,"
            + COLUMN_NAME_ALARM_DAYS + " INTEGER," + " )";

    private static final String SQL_DELETE_ALARM = "DROP TABLE IF EXISTS "
            + TABLE_NAME;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ALARM);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ALARM);
    }

    private AlarmModel populateModel(Cursor c) {
        AlarmModel model = new AlarmModel();
        model.id = c.getInt(c.getColumnIndex(ID));
        model.timeHour = c
                .getInt(c.getColumnIndex(COLUMN_NAME_ALARM_TIME_HOUR));
        model.timeMinute = c.getInt(c
                .getColumnIndex(COLUMN_NAME_ALARM_TIME_MINUTE));
        model.day = c.getInt(c.getColumnIndex(COLUMN_NAME_ALARM_DAYS));

        return model;
    }

    private ContentValues populateContent(AlarmModel model) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_ALARM_TIME_HOUR, model.timeHour);
        values.put(COLUMN_NAME_ALARM_TIME_MINUTE, model.timeMinute);
        values.put(COLUMN_NAME_ALARM_DAYS, model.day);
        return values;
    }

    public long createAlarm(AlarmModel model) {
        ContentValues values = populateContent(model);
        return getWritableDatabase().insert(TABLE_NAME, null, values);
    }

    public AlarmModel getAlarm(long id) {
        SQLiteDatabase db = this.getReadableDatabase();

        String select = "SELECT * FROM " + TABLE_NAME + " WHERE " + ID + " = "
                + id;

        Cursor c = db.rawQuery(select, null);

        if (c.moveToNext()) {
            return populateModel(c);
        }

        return null;
    }

    public long updateAlarm(AlarmModel model) {
        ContentValues values = populateContent(model);
        return getWritableDatabase().update(TABLE_NAME, values, ID + " = ?",
                new String[] { String.valueOf(model.id) });
    }

    public int deleteAlarm(long id) {
        return getWritableDatabase().delete(TABLE_NAME, ID + " = ?",
                new String[] { String.valueOf(id) });
    }

    public List<AlarmModel> getAlarms() {
        SQLiteDatabase db = this.getReadableDatabase();

        String select = "SELECT * FROM " + TABLE_NAME;

        Cursor c = db.rawQuery(select, null);

        List<AlarmModel> alarmList = new ArrayList<AlarmModel>();

        while (c.moveToNext()) {
            alarmList.add(populateModel(c));
        }

        if (!alarmList.isEmpty()) {
            return alarmList;
        }

        return null;
    }
}

0 个答案:

没有答案