解析java.lang.NoClassDefFoundError

时间:2016-05-16 04:12:41

标签: java android classnotfoundexception

我有这个班级

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)

我知道有一些类似的问题,但我认为这是个案的问题,

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在类DbHelper中

你不能给出与类名相同的对象名,所以你必须改变对象的名称,如

DaoMaster daoMaster;
DaoSession daoSession;

答案 1 :(得分:0)

我面临同样的问题,我的DBHelper calss没有加载抛出错误

java.lang.NoClassDefFoundError

在提到的评论1214中,这可能是问题too many methods,这是与内存相关的问题。所以我在一台具有小内存API版本19的设备上进行了测试,它抛出了java.lang.NoClassDefFoundError,但同样的应用程序可以在其他具有API 21的设备上工作,并且比普及设备更好的内存硬件。

因此,这必须是内存问题或Android版本问题。