Reputation: 33
So when I launch the app I get the fatal exception main saying that there is no empty constructor. Then when I add the empty constructor it gives me that "The constructor SQLiteOpenHelper() is undefined" error.
This is my first app ever so I have no clue what to do.
Here's the code:
package com.example.lightalarmclock;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
public class writeAlarm extends Activity {
public static final String PREFS = "positions";
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private DbHelper ourHelper;
private static String pos1 = "0";
private static String pos2 = "0";
private static String pos3 = "0";
private static String pos4 = "0";
private static String timeSet;
private static int rep1 = 0;
private static int rep2 = 0;
private static int rep3 = 0;
private static int rep4 = 0;
private static int rep5 = 0;
private static int rep6 = 0;
private static int rep7 = 0;
private static int repB;
private static int repSet;
private static final String DATABASE_NAME = "alarmDB";
private static final String DATABASE_TABLE = "alarmStack";
private static final int DATABASE_VERSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void updateVars() {
SharedPreferences positions = getSharedPreferences(PREFS, 0);
//TODO import from sharedPreferences to local vars
}
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DbHelper() {
super();
}
@Override
public void onCreate(SQLiteDatabase db) {
//writing to database
if (rep1 >= 0 || rep2 >= 0 || rep3 >= 0 || rep4 >= 0 || rep5 >= 0 || rep6 >= 0 || rep7 >= 0) {
repB = 1;
}
else {
repB = 0;
}
//setting time
timeSet= pos4+pos3+":"+pos2+pos1;
//Writing
db.execSQL
("INSERT INTO TABLE alarmDB.alarmStack(alarm_time, alarm_repeat, rep1, rep2, rep3, rep4, rep5, rep6, rep7) " +
"VALUES (" + timeSet + ", " + repB + ", "+ rep1 +", "+ rep2 +", "+ rep3 +", "+ rep4 +", "+ rep5 +", "+ rep6 +", "+ rep7 +")");
//launch backToMain
backToMain(null);
}
public static void backToMain(Context ctx) {
// do some stuff here
ctx.startActivity(new Intent(ctx, MainActivity.class));
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public void backToMain() {
Intent j = new Intent(writeAlarm.this, MainActivity.class);
startActivity(j);
}
public writeAlarm (Context c) {
ourContext = c;
}
public writeAlarm open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
}
Upvotes: 0
Views: 4543
Reputation: 133560
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Look at he public ocnstrucors and pass right parms
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
Create a helper object to create, open, and/or manage a database.
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)
Create a helper object to create, open, and/or manage a database.
You need to use one of the above two constructors. But you have
public DbHelper() { // remove this
super();
}
You already have
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
Edit:
You are creating a constructor of Activity
class which is wrong
public class writeAlarm extends Activity {
You have
public writeAlarm (Context c) {
ourContext = c;
}
I would suggest you to move this
private static class DbHelper extends SQLiteOpenHelper {
to separate .java file
Upvotes: 1
Reputation: 665
I m showing one exmample For SqlLiteHelper
1)Creating class for sqlitehelper
public class SQLHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "myDatabase";
public static final String TBL_NAME = "myTable";
public static final String COL1 = "ID";
public static final String COL2 = "Name";
public static final String COL3 = "Mobile";
public SQLHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createTable Query);
Log.d("tag", "Succesffull");
}
public void InsertData(String val1,String val2) {
SQLiteDatabase db = getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL2, val1);
cv.put(COL3, val2);
db.insert(TBL_NAME, COL1, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}}
After Created your class after calling //initialize SQLHelper class
SQLHelper helper;
//Simply pass your current activity
helper = new SQLHelper(MainActivity.this);
Upvotes: 0
Reputation: 6179
Too many things!!
Remove this:
public DbHelper() {
super();
}
And this:
public writeAlarm (Context c){
ourContext = c;
}
At onCreate put this:
ourContext = this;
In
@Override
public void onCreate(SQLiteDatabase db) {
You must create the database.
Upvotes: 0
Reputation: 10540
For class DbHelper extends SQLiteOpenHelper
you are doing this:
public DbHelper() {
super();
}
If you look in the Android docs for SQLiteOpenHelper, there is no SQLiteOpenHelper()
constructor so it is complaining.
Upvotes: 3