无法实例化userSQLOpenHelper类型,我不知道为什么

时间:2014-09-23 16:48:55

标签: android database sqlite

在我的MainActivity.java中,我收到此错误“无法实例化userSQLOpenHelper类型”。在新的userSQLOpenHelper(this)。有人能告诉我我错过了什么或做错了吗?

MainActivity.java

userSQLOpenHelper db = new userSQLOpenHelper(this);             
// add user
db.addUser(new user(name1,born1,sex,passwd,email1));   

user.java

public final class user {

    private int id;
    private String name;
    private String born;
    private String sex;
    private int password;
    private String email;

        // This is recommended to prevent someone from accidentally instantiating the user class,
    public user() {}

    /* Inner class that defines the table contents */
    public user(String name, String born, String sex, int password, String email)      {
        // super();
        this.name = name;
        this.born = born;
        this.sex = sex;
        this.password = password;
        this.email = email;
    }

    @Override
    public String toString() {
        return "user [id=" + id + ", name=" + name + ", born=" + born + ", sex=" + sex + ", password=" + password
            + ", email=" + email + "]";
    }

    public int getId(){
        return this.id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getName(){
        return this.name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getBorn(){
        return this.born;
    }

    public void setBorn(String born){
        this.born = born;
    }

      public String getSex(){
        return this.sex;
    }

    public void setSex(String sex){
        this.sex = sex;
    }

      public int getPassword(){
        return this.password;
    }

    public void setPassword(int password){
        this.password = password;
    }

    public String getEmail(){
        return this.email;
    }

    public void setEmail(String email){
        this.email = email;
    }
}

userSQLOpenHelper.java

public abstract class userSQLOpenHelper extends SQLiteOpenHelper {

    // Table name
    private static final String TABLE_USERS = "users";

    // Table Columns names
    private static final String USER_ID = "id";
    private static final String USER_NAME = "name";
    private static final String USER_BORN = "born";
    private static final String USER_SEX = "sex";
    private static final String USER_PASSWORD = "password";
    private static final String USER_EMAIL = "email";

    //private static final String[] COLUMNS = {USER_ID,USER_NAME,USER_BORN,USER_SEX,USER_PASSWORD,USER_EMAIL};

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "userDB";   

  public userSQLOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  }


    @Override
  public void onCreate(SQLiteDatabase db) {
    // SQL statement to create user table
    String CREATE_USER_TABLE = "CREATE TABLE users ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            "name TEXT NOT NULL, "+ 
            "born TEXT NOT NULL, " +
            "sex TEXT NOT NULL, " +
            "password INTERGER NOT NULL, " +
            "email TEXT NOT NULL )";

    // create books table
    db.execSQL(CREATE_USER_TABLE);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older books table if existed
    db.execSQL("DROP TABLE IF EXISTS books");

    // create fresh books table
    this.onCreate(db);
  }


  public void addUser(user user){
    SQLiteDatabase db;
     //get reference to writable DB
     db = this.getWritableDatabase();

    //create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(USER_NAME, user.getName()); 
    values.put(USER_BORN, user.getBorn()); 
    values.put(USER_SEX, user.getSex()); 
    values.put(USER_PASSWORD, user.getPassword()); 
    values.put(USER_EMAIL, user.getEmail()); 

      // insert
    db.insert(TABLE_USERS, null, values); 

    // close
    db.close(); 
    }

}

2 个答案:

答案 0 :(得分:0)

您已宣布您的课程抽象。

从userSQLOpenHelper类声明中删除该关键字。

答案 1 :(得分:0)

您无法实例化abstract类...

public abstract class userSQLOpenHelper

由于您的类中没有任何abstract方法,因此您只需从类声明中删除关键字abstract,该错误就会消失......