mardi 24 novembre 2015

NoClassDefFoundError for subclass of SqliteOpenHelper

I am building an application which uses SqliteDatabase.

The structure of my project goes on something like this: MyDB.java which gives access to the database. MyDB.java has an inner private class known as MyDBHelper.java, which extends SQLiteOpenHelper.java.

I have added the constructors for these classes below:

public class MyDB {
    public static MyDB getDatabase(Context context) {
        if (mDB == null) {
            mDB = new MyDB(context.getApplicationContext());
        }
        return mDB;
    }

    private MyDB(Context context) {
        mHelper = new MyDBHelper(context);
        mHelper.getWritableDatabase();
    }

    private class MyDBHelper extends SQLiteOpenHelper {

        public MyDBHelper(Context context) {
            super(context, "DATABASE_NAME", null, 10); //10 being the version name of database
        }
    }
}

While launcing the app in debug mode, I am getting NoClassDefFoundError. I tested the app on three devices: one running on Android 5.0, one running Android 4.2.1 and other running Android 4.2.1 (tablet). The wierd part is that I am getting this exception on Android 4.2.1 while the app runs perfectly on Android 5.0. Also if I download the app from play store on Jelly Bean devices, it runs smoothly.

I have added the complete stacktrace below:

11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime: FATAL EXCEPTION: main
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime: java.lang.NoClassDefFoundError: com.My.andorid.app.My.utils.MyDB$MyDBHelper
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at com.My.andorid.app.My.utils.MyDB.<init>(MyDB.java:101)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at com.My.andorid.app.My.utils.MyDB.getDatabase(MyDB.java:95)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at com.My.andorid.app.My.ui.SplashActivity.onCreate(SplashActivity.java:121)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5020)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:149)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:153)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4987)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
11-24 15:27:40.987 21945-21945/com.My.andorid.app.My E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

Can anybody tell me the reason of this issue, and also how to resolve this problem. I need to test the app on pre-lollipop devices.

Aucun commentaire:

Enregistrer un commentaire