本文介绍了构造函数SQLiteOpenHelper()是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,当我启动应用程序我得到的致命异常主要说没有空的构造。
然后,当我加空构造它给我说:​​构造SQLiteOpenHelper()是未定义的错误。

这是我的第一个应用程序不断,所以我不知道该怎么做。

这里的code:

 包com.example.lightalarmclock;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.os.Bundle;公共类writeAlarm延伸活动{    公共静态最后弦乐preFS =阵地;
    私人最终上下文ourContext;
    私人SQLiteDatabase ourDatabase;
    私人DbHelper ourHelper;    私人静态字符串POS1 =0;
    私人静态字符串POS2 =0;
    私人静态字符串POS3 =0;
    私人静态字符串POS4 =0;
    私人静态字符串timeSet;    私有静态诠释REP1 = 0;
    私有静态诠释REP2 = 0;
    私有静态诠释rep3 = 0;
    私有静态诠释rep4 = 0;
    私有静态诠释rep5 = 0;
    私有静态诠释rep6 = 0;
    私有静态诠释rep7 = 0;
    私有静态诠释repB;
    私有静态诠释repSet;    私有静态最后弦乐DATABASE_NAME =alarmDB;
    私有静态最后弦乐DATABASE_TABLE =alarmStack;
    私有静态最终诠释DATABASE_VERSION = 1;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
    }    公共无效updateVars(){
        共享preferences位置= getShared preferences(preFS,0);
        //从共享preferences到本地变量TODO进口
    }    私有静态类DbHelper扩展SQLiteOpenHelper {
        公共DbHelper(上下文的背景下){
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }        公共DbHelper(){
            超();
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB){
            //写入数据库
            如果(REP1> = 0 || REP2> = 0 || rep3> = 0 || rep4> = 0 || rep5> = 0 || rep6> = 0 || rep7> = 0) {
                repB = 1;
            }
            其他{
                repB = 0;
            }            //设置时间
            timeSet = POS4 + POS3 +:+ POS2 + POS1;            //写作
            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 +));            //推出backToMain
            backToMain(NULL);
        }        公共静态无效backToMain(上下文CTX){
            //做一些东西在这里
            ctx.startActivity(新意图(CTX,MainActivity.class));
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            db.execSQL(DROP TABLE IF EXISTS+ DATABASE_TABLE);
            的onCreate(DB);
        }
    }    公共无效backToMain(){
        意向J =新意图(writeAlarm.this,MainActivity.class);
        startActivity(J);
    }    公共writeAlarm(上下文C){
        ourContext = C;
    }    公共writeAlarm的open(){
        ourHelper =新DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        返回此;
    }    公共无效的close(){
        ourHelper.close();
    }
}


解决方案

对于类 DbHelper扩展SQLiteOpenHelper 你这样做是:

 公共DbHelper(){
    超();
}

如果您在 Android的文档,没有 SQLiteOpenHelper()的构造,因此在抱怨。

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();
    }
}
解决方案

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.

这篇关于构造函数SQLiteOpenHelper()是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 16:19