构造函数SQLiteOpenHelper()未定义

时间:2013-12-13 20:35:28

标签: android undefined android-sqlite

因此,当我启动应用程序时,我得到致命的异常主要说没有空的构造函数。 然后,当我添加空构造函数时,它给出了“构造函数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();
    }
}

4 个答案:

答案 0 :(得分:3)

对于班级DbHelper extends SQLiteOpenHelper,你这样做:

public DbHelper() {
    super();
}

如果查看Android docs for SQLiteOpenHelper,则没有SQLiteOpenHelper()构造函数,因此抱怨。

答案 1 :(得分:1)

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

看看他是公共的指导者并传递正确的参与者

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

创建一个帮助对象来创建,打开和/或管理数据库。

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)

创建一个帮助对象来创建,打开和/或管理数据库。

您需要使用以上两个构造函数之一。但你有

public DbHelper() { // remove this
        super(); 
    }

你已经拥有

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

编辑:

您正在创建Activity类的构造函数,这是错误的

public class writeAlarm extends Activity {

你有

public writeAlarm (Context c) {
        ourContext = c;
    }

我建议你移动这个

 private static class DbHelper extends SQLiteOpenHelper {

分隔.java文件

答案 2 :(得分:0)

太多东西!!

删除它:

public DbHelper() {
    super();
}  

而且:

public writeAlarm (Context c){
    ourContext = c;
}  

在onCreate上放了这个:

ourContext = this;  

@Override
public void onCreate(SQLiteDatabase db) {  

您必须创建数据库。

See this maybe help

答案 3 :(得分:0)

我为SqlLiteHelper显示了一个例子

1)为sqlitehelper创建类

  

public class SQLHelper扩展了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

}}

在打电话后创建你的课程 //初始化SQLHelper类

SQLHelper帮助程序;

//简单地传递您当前的活动

helper = new SQLHelper(MainActivity.this);