"这个类应该有一个默认的构造函数" [Android]产品

时间:2014-11-21 15:27:30

标签: android class constructor default

每当我尝试在Android Studio中生成已签名的APK时,我都会收到此错误。

Error:(69) Error: This class should provide a default constructor (a public constructor with no arguments) (com.example.msdproject.DBManager) [Instantiatable]

谁能告诉我为什么会这样?

以下是造成问题的类:

package com.example.msdproject;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;

import java.sql.SQLException;


public class DBManager {

    public static final String COL_ROWID = "_id";
    public static final String COL_NAME = "name";
    public static final String COL_VENUE = "venue";
    public static final String COL_DATE = "date";
    public static final String COL_COMMENTS = "comments";

    private static final String DB_NAME = "Concerts";
    private static final String DB_TABLE = "Concert_Info";
    private static final int DB_VERSION = 1;

    private static final String DB_CREATE =
            "create table " + DB_TABLE +
    " (_id integer primary key autoincrement, " +
            "name text not null, " +
            "venue text not null, " +
            "comments text not null, " +
            "date text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBManager(Context ctx)
    {
        super();
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    public static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            //to do
        }
    }

    public DBManager open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        DBHelper.close();
    }

    public long insertConcert(String name, String venue, String date, String comments)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(COL_NAME, name);
        initialValues.put(COL_VENUE, venue);
        initialValues.put(COL_DATE, date);
        initialValues.put(COL_COMMENTS, comments);
        return db.insert(DB_TABLE, null, initialValues);
    }

    public boolean deleteConcert(String name)
    {
        return db.delete(DB_TABLE, "name='"+ name+"'", null) > 0;

    }

    public Cursor getAllConcerts() {
        return db.query(DB_TABLE, new String[]
                        {
                                COL_ROWID,
                                COL_NAME,
                                COL_VENUE,
                                COL_DATE,
                                COL_COMMENTS
                        },
                        null,
                        null,
                        null,
                        null,
                        COL_NAME+ " ASC"
                );
    }

    public Cursor getConcert(long ROW_ID) throws SQLException
    {
        Cursor mCursor =
                db.query(DB_TABLE, new String[]
                        {
                                COL_ROWID,
                                COL_NAME,
                                COL_VENUE,
                                COL_DATE,
                                COL_COMMENTS
                        },
                        COL_ROWID + "=" + ROW_ID,
                        null,
                        null,
                        null,
                        null
                );
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }

        return mCursor;
    }

    public boolean updateConcert(String name, String venue, String date, String comments)
    {
        ContentValues updateValues = new ContentValues();
        updateValues.put(COL_NAME, name);
        updateValues.put(COL_VENUE, venue);
        updateValues.put(COL_DATE, date);
        updateValues.put(COL_COMMENTS, comments);
        return db.update(DB_TABLE, updateValues, "name='"+ name+"'", null) > 0;
    }

    public void reset () throws SQLException {
        db.delete(DB_TABLE, null, null);
        db.close();
        this.DBHelper.onCreate(this.db);
    }
}

3 个答案:

答案 0 :(得分:3)

public DBManager(Context ctx)
{
    super(); // Remove this
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

删除super();您不会扩展任何课程。

同时删除final,因此您应该private Context context;

答案 1 :(得分:1)

运行时需要一个空构造函数来实例化该类而不需要参数。只需添加一个空构造函数public DBManager (){}

答案 2 :(得分:-1)

public final class UserProfile {

private UserProfile(){

}

public static UserProfile getProfile(){

    UserProfile userProfile = new UserProfile();
    return userProfile;
}
class Users implements BaseColumn{

    public static final String TABLE_NAME = "UserInfo";
    public static final String COL_ID = "_ID";
    public static final String COL_USERNAME  = "userName ";
    public static final String COL_DOB = "dateOfBirth";
    public static final String COL_GENDER = "Gender";
    public static final String COL_PASSWORD = "Password";

    private int id;
    private String username;
    private String dob;
    private String gender;
    private String password;


    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }

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

public Users getUser(){
    Users users = new Users();

    return users;
}



}