我有这个班
public class DaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1605131322;
/** Creates underlying database table using DAOs. */
public static void createAllTables(SQLiteDatabase db, boolean ifNotExists) {
ApplicationConfigDao.createTable(db, ifNotExists);
RequestCacheDao.createTable(db, ifNotExists);
}
/** Drops underlying database table using DAOs. */
public static void dropAllTables(SQLiteDatabase db, boolean ifExists) {
ApplicationConfigDao.dropTable(db, ifExists);
RequestCacheDao.dropTable(db, ifExists);
}
public static abstract class OpenHelper extends SQLiteOpenHelper {
public OpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("greenDAO", "Creating tables for schema version " + SCHEMA_VERSION);
createAllTables(db, false);
}
}
/** WARNING: Drops all table on Upgrade! Use only during development. */
public static class DevOpenHelper extends OpenHelper {
public DevOpenHelper(Context context, String name, CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
dropAllTables(db, true);
onCreate(db);
}
}
public DaoMaster(SQLiteDatabase db) {
super(db, SCHEMA_VERSION);
registerDaoClass(ApplicationConfigDao.class);
registerDaoClass(RequestCacheDao.class);
}
public DaoSession newSession() {
return new DaoSession(db, IdentityScopeType.Session, daoConfigMap);
}
public DaoSession newSession(IdentityScopeType type) {
return new DaoSession(db, type, daoConfigMap);
}
}
然后是另一个班
更新
正如你们中的一些人所指出的,我的命名约定似乎不好,多么愚蠢的基本错误,所以我将它们更新为
public class DbHelper {
public static final String LOGIN_DATA_PREFIX = "LOGIN_DATA_";
public DaoMaster DAOMaster;
public DaoSession DAOSession;
public DaoMaster.OpenHelper openHelper;
public SQLiteDatabase SQLiteDb;
private Context context;
public static class CONSTANT {
public static String DB_FILE_NAME = "app.db";
/* Define all DAO Property Constant Here */
public static class APPLICATION_CONFIG {
public static String DAO_NAME = "applicationconfig";
public static String PROPERTY_ID = "Id";
public static String PROPERTY_KEY = "Key";
public static String PROPERTY_VALUE = "Value";
}
public static class REQUEST_CACHE{
public static String DAO_NAME = "requestcache";
public static String PROPERTY_URL = "Url";
public static String PROPERTY_METHOD = "Method";
public static String PROPERTY_PARAM = "Param";
public static String PROPERTY_ID = "Id";
}
//...
}
public class DAOWrapper {
private DaoSession session;
/* Load all DAO Here */
public ApplicationConfigDao ApplicationConfig;
public RequestCacheDao RequestCache;
// ...
public DAOWrapper(DaoSession session){
this.session = session;
/* Init all DAO Here */
this.ApplicationConfig = this.session.getApplicationConfigDao();
this.RequestCache = this.session.getRequestCacheDao();
// ...
}
}
public DAOWrapper DAO;
public DbHelper(Context context){
this.context = context;
this.openHelper = new DaoMaster.DevOpenHelper(this.context, CONSTANT.DB_FILE_NAME, null);
this.SQLiteDb = this.openHelper.getWritableDatabase();
this.DAOMaster = new DaoMaster(this.SQLiteDb);
this.DAOSession = this.DAOMaster.newSession();
this.DAO = new DAOWrapper(this.DAOSession);
}
...
}
但还是失败了
然后初始化类
this.Db = new DbHelper(this.getApplicationContext());
但是它在创建实例daomaster.devopenhelper时抛出错误,据说找不到类devopenhelper,我的代码怎么了?
更新
错误控制台
5-15 23:22:31.313 20424-20424/com.itp.android.suitetreatadmin E/dalvikvm﹕ Could not find class 'com.itp.android.suitetreatadmin.dao.DaoMaster$DevOpenHelper', referenced from method com.itp.android.suitetreatadmin.DbHelper.<init>
05-15 23:22:44.190 20424-20424/com.itp.android.suitetreatadmin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.itp.android.suitetreatadmin, PID: 20424
java.lang.NoClassDefFoundError: com.itp.android.suitetreatadmin.dao.DaoMaster$DevOpenHelper
at com.itp.android.suitetreatadmin.DbHelper.<init>(DbHelper.java:86)
at com.itp.android.suitetreatadmin.App.onCreate(App.java:108)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4328)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我知道有一些类似的问题,但我认为这是个案问题,
谢谢你的帮助。
最佳答案
在类dbhelper中,不能给出与类名相同的对象名,因此必须像
DaoMaster daoMaster;
DaoSession daoSession;