【发布时间】:2013-12-13 20:35:27
【问题描述】:
所以当我启动应用程序时,我得到一个致命异常 main 说没有空的构造函数。 然后,当我添加空构造函数时,它给了我“构造函数 SQLiteOpenHelper() 未定义”错误。
这是我的第一个应用程序,所以我不知道该怎么做。
代码如下:
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();
}
}
【问题讨论】:
-
@natez0r 正如我在开头所写的那样,当我删除构造时,我得到一个运行时错误,说“没有空的构造函数”
标签: android undefined android-sqlite