构造函数DbHelper(CustomListPreference)未定义

时间:2013-12-19 10:34:41

标签: android listpreference

请告诉我语法有什么问题。我收到错误'构造函数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);

    }

}

1 个答案:

答案 0 :(得分:0)

您正在传递ListPreference个对象,而您的DBHelper需要Context。见这一行:

public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

ListPreference不是Context的子类,因此出现此错误。您将重新审视您的设计。

更新

您应该将此行更改为:

DbHelper dbHelp = new DbHelper(mContext);