我有這個類解決java.lang.NoClassDefFoundError
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);
}
}
然後另一個類
UPDATE 一些你當成指出,看來我的命名約定是壞的,多麼愚蠢基本的錯誤,所以我把它們更新到以下
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,什麼是錯我的代碼?
UPDATE 錯誤控制檯
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)
我知道有一些類似的問題,但我認爲這是問題的情況下,
感謝您的幫助。
首先嚐試從Gradle中使用乾淨和組裝,如果您使用AndroidStudio – Konstantin
您的命名約定不好..您不能給出與類名相同的變量名。 –
更新,我修改它們,但它仍然拋出相同的錯誤 – 1412