2014-12-27 36 views
7

嘗試使用SQLiteOpenHelper擴展類,但此錯誤顯示爲:「android.database.sqlite.SQLitepenhelper中沒有可用的默認構造函數「與其他一起‘不能解析符號類別,注意,...’Android Studio中的「android.database.sqlite.SQLitepenhelper中沒有默認構造函數」

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); 
    } 

回答

13

您需要定義調用SQLiteOpenHelper的4或5 ARG super構造一個顯式構造自己。

例如:

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

其中database.db是你的數據庫文件名和1是版本。

+0

我有同樣的問題WebView類,但無論如何這回答了我的問題。謝謝! – Seth 2016-08-04 22:02:26

2

如果你的孩子DBHelper然後這個帖子的幫助,othervise可以媒體鏈接understandfirst定義它像這樣的你在課堂之外,意味着uperside ...

private DBHelper ourHelper; 
private final Context ourContext; 

然後用這個

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); 
} 

然後嘗試此上下文爲您的父類

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