hi i am creating android alarm aap in that i have to store alarms in the sqlite database i cannot able to store data here the following is my mainactivity and database activity please help me to solve the below last given error.? Thank you in advance. This is my mainactivity databse code.
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "+ targetCal.getTime() + "\n" + "Repeat every 5 minutes\n"+ "***\n");
long hour = targetCal.getTimeInMillis();
long minute = targetCal.getTimeInMillis();
Intent i = new Intent(this, DataBaseActivity.class);
i.putExtra("hour", hour);
i.putExtra("minute", minute);
startActivity(i);
and This is my database activity.
public abstract class DataBaseActivity extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "alarmclock";
private static final String CREATE_TABLE = "alarm";
private static String id;
private Long hour;
private Long minute;
private static final String KEY_ID = id;
private static final String KEY_ALARM_HOUR = "hour";
private static final String KEY_ALARM_MINUTE = "minute";
public DataBaseActivity(Context context, String name,CursorFactory factory, int version) {
super(context, name, factory, version);
// Intent intent = new Intent();
// intent.getLongExtra("hour", hour);
// intent.getLongExtra("minute", minute);
SQLiteDatabase db = this.getWritableDatabase();
final String CREATE_TABLE_TODO = "CREATE TABLE " + "(" + KEY_ID
+ "INTEGER AUTO INCRIMENT," + KEY_ALARM_HOUR + "TIME,"
+ KEY_ALARM_MINUTE + "TIME" + ")";
ContentValues values = new ContentValues();
values.put(KEY_ALARM_HOUR, hour); // Contact Name
values.put(KEY_ALARM_MINUTE, minute);
db.insert(CREATE_TABLE, null, values);
db.close();
}
}
and the error i am getting is as follows.
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.toprecur.alarmactivity/com.toprecur.alarmactivity.DataBaseActivity}: java.lang.InstantiationException: can't instantiate class com.toprecur.alarmactivity.DataBaseActivity
This is the simplified version for a database implementation I have for storing alarms.
public class AlarmsOpenHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "ALARMS";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_HOUR = "HOUR";
public static final String COLUMN_MINUTE = "MINUTE";
public static final String COLUMN_DATE = "DATE";
public static final String COLUMN_NAME = "NAME";
public static final String COLUMN_TONE = "TONE";
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "alarm_app.db";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_HOUR + " TEXT NOT NULL, " +
COLUMN_MINUTE + " TEXT NOT NULL, " +
COLUMN_DATE + " TEXT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_TONE + " TEXT);";
AlarmsOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
Then on the activity you wish to play with the database:
static AlarmsOpenHelper alarmsOpenHelper;
static SQLiteDatabase db;
You can make an initDatabase method as I do
private void initDatabase() {
alarmsOpenHelper = new AlarmsOpenHelper(getApplicationContext());
db = alarmsOpenHelper.getWritableDatabase();
}
From here you can play with the database as you wish, like for example
static void addAlarmToDatabase(String hour, String minute) {
db.execSQL("INSERT INTO " + AlarmsOpenHelper.TABLE_NAME +
" (" + AlarmsOpenHelper.COLUMN_HOUR + ", " +
AlarmsOpenHelper.COLUMN_MINUTE + ") VALUES ('" +
hour + "', '" + minute + "');");
}
Another thing is you can mess your database up, for reconstructing the tables just change de database version, for example in this case would be changing
private static final int DATABASE_VERSION = 3;
to
private static final int DATABASE_VERSION = 4;
That will call the onUpgrade method.
Read some documentation and you'll get the hints Using Databases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With