“这个类应该有一个默认构造函数”[Android]
Posted
技术标签:
【中文标题】“这个类应该有一个默认构造函数”[Android]【英文标题】:"This class should have a default constructor" [Android] 【发布时间】:2014-11-21 15:27:30 【问题描述】:每当我尝试在 android Studio 中生成签名的 APK 时,我都会收到此错误。
Error:(69) Error: This class should provide a default constructor (a public constructor with no arguments) (com.example.msdproject.DBManager) [Instantiatable]
谁能告诉我为什么会这样?
这是导致问题的类:
包 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);
【问题讨论】:
最适合我的解决方案this 【参考方案1】:public DBManager(Context ctx)
super(); // Remove this
this.context = ctx;
DBHelper = new DatabaseHelper(context);
删除 super();
你没有扩展任何类。
同时删除final
,所以你应该有private Context context;
【讨论】:
我假设这是作为一般性更正而不是修复,因为我仍然遇到同样的错误。不管怎样,谢谢! 是的,从上下文中删除最终属性。【参考方案2】:运行时需要一个空的构造函数来实例化类而不需要参数。只需添加一个空的构造函数public DBManager ()
【讨论】:
我添加了这个,Android Studio 在“private final Context context;”这一行给了我一个错误说变量“上下文”可能没有被初始化,建议之一是将它放在新创建的 DBManager 方法中并将其初始化为空。这是正确的吗? 删除最后。无论在什么条件下,最终变量都必须是初始化之王。你关心在哪里使用它而不是散布它是很好的。请阅读并保持良好的工作【参考方案3】: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;
【讨论】:
虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高答案的长期价值。以上是关于“这个类应该有一个默认构造函数”[Android]的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )
Android 逆向Android 权限 ( Android 逆向中使用的 android.permission 权限 | Android 系统中的 Linux 用户权限 )