如何在片段中声明扩展SQLiteOpenHelper的类的变量?

时间:2015-04-16 18:35:28

标签: android android-fragments sqliteopenhelper

我必须在扩展Fragment的类中声明以下变量:

public class CarrelloFragment  extends Fragment {

DatabaseHelper sqLite=new DatabaseHelper(this); //here is the error

DatabaseHelper类的构造函数不允许使用片段执行此操作。

以下是扩展SQLiteOpenHelper的DatabaseHelper类的构造函数的代码:

公共类DatabaseHelper扩展SQLiteOpenHelper {     private static final String DATABASE_NAME =“MySupermarket.db”;

private static final int SCHEMA_VERSION = 2;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

有没有办法在片段类中声明DatabaseHelper变量?

1 个答案:

答案 0 :(得分:2)

尝试DatabaseHelper sqLite=new DatabaseHelper(getActivity());