1

I keep getting this error whenever I try and generate a signed APK in Android Studio.

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

Can anyone tell me why this is happening?

Here is the class that's causing the problem:

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);
    }
}
bbb
  • 1,479
  • 2
  • 15
  • 28
  • Best solution works for me [this](http://stackoverflow.com/questions/17420637/error-non-default-constructors-in-fragments/39608360#39608360) – Naeem Ibrahim Sep 21 '16 at 05:48

3 Answers3

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

remove super(); you do not extend any class.

Also remove final, so you should have private Context context;

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • I'm assuming this was intended as a general correction rather than a fix because I'm still getting the same error. Thanks regardless! – bbb Nov 21 '14 at 15:40
  • 1
    Yes, remove the final attribute from Context. – Carnal Nov 21 '14 at 15:42
1

The runtime needs an empty constructor to instantiate the class with no need of parameters. Just add an empty constructorpublic DBManager (){}

eduyayo
  • 2,020
  • 2
  • 15
  • 35
  • I added this in and Android Studio gave me an error on this line "private final Context context;" saying that the variable 'context' may not have been intialised and one of the suggestions was to place it in the newly created DBManager method and initialise it to null. Is this correct? – bbb Nov 21 '14 at 15:36
  • Remove the final. Final vars must be king of initialized no matter what conditions. It's good that you care where to use it and not spread it around. Please read about it and keep the good work – eduyayo Nov 21 '14 at 19:13
-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;
}



}
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Oct 14 '18 at 01:30