“There is no default constructor available in android.database.sqlite.SQLitepenhelper” in Android Studio

﹥>﹥吖頭↗ 提交于 2019-12-01 15:19:24

问题


Trying to extend class with SQLiteOpenHelper, but this error shows up : "There is no default constructor available in android.database.sqlite.SQLitepenhelper" along with other "cannot resolve symbol Category, Note,..."

class DbHelper extends SQLiteOpenHelper {


    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(Category.getSql());
        db.execSQL(Note.getSql());
        db.execSQL(Attachment.getSql());
        db.execSQL(CheckItem.getSql());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Category.TABLE_NAME);
       db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Attachment.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + CheckItem.TABLE_NAME);

        onCreate(db);
    }

回答1:


You need to define an explicit constructor yourself that calls the 4- or 5-arg super constructor in SQLiteOpenHelper.

For example:

public DbHelper(Context context) {
    super(context, "database.db", null, 1);
}

where database.db is your database file name and 1 is the version.




回答2:


If your DBHelper child then this post help, othervise you can allready understandfirst Define it like this one in outside of you class, means uperside...

private DBHelper ourHelper;
private final Context ourContext;

Then use this

class DbHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VIRSION);
        // TODO Auto-generated constructor stub
    }

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(Category.getSql());
    db.execSQL(Note.getSql());
    db.execSQL(Attachment.getSql());
    db.execSQL(CheckItem.getSql());
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Category.TABLE_NAME);
   db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + Attachment.TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + CheckItem.TABLE_NAME);

    onCreate(db);
}

And then try this context for your parent class

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


来源:https://stackoverflow.com/questions/27667914/there-is-no-default-constructor-available-in-android-database-sqlite-sqlitepenh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!