2014-09-23 76 views
1

在我的MainActivity.java中,出現此錯誤「無法實例化類型userSQLOpenHelper」。在新的userSQLOpenHelper(this)。有人能告訴我我缺少什麼或做錯了什麼嗎?無法實例化類型userSQLOpenHelper,我不知道爲什麼

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

} 
+1

請參閱[採訪:我們能否實例化抽象類?](http://stackoverflow.com/questions/13670991/interview-can-we-instantiate-abstract-class) – 2014-09-23 16:52:11

回答

0

無法實例abstract類...

public abstract class userSQLOpenHelper 

既然你沒有你的類內的任何abstract方法,你可以從你的類聲明中刪除關鍵字abstract和錯誤應該消失...

+0

謝謝。這工作。 – JulieB 2014-09-23 18:24:48

0

你宣佈你的抽象類。

從您的userSQLOpenHelper類聲明中刪除該關鍵字。