请告诉我语法有什么问题。我收到错误'构造函数DbHelper(CustomListPreference)未定义' 这是我的代码:
CustomListPreference.java
public class CustomListPreference extends ListPreference {
CustomListPreferenceAdapter customListPreferenceAdapter = null;
Context mContext;
private SQLiteDatabase db;
DbHelper dbHelp = new DbHelper(this); (Here I get an error)
...
DbHelper.java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="myBase";
public static final String KEY_NAME="name";
public static final String KEY_ID="id";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table tUser ("
+ "id integer primary key autoincrement,"
+ "name text,"
+ "exists integer" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:0)
您正在传递ListPreference
个对象,而您的DBHelper需要Context
。见这一行:
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
ListPreference
不是Context
的子类,因此出现此错误。您将重新审视您的设计。
您应该将此行更改为:
DbHelper dbHelp = new DbHelper(mContext);