I am trying to instantiate a class on my applications onCreate
method as follows:
volleyQueueInstance = VolleySingleton.getInstance(getApplicationContext());
Following is my VolleySingleton
class
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private VolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(30);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
But my app crashes on launch with the following stacktrace that says no class definition found for class VolleySingleton
java.lang.NoClassDefFoundError: rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton$1
at rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.<init>(VolleySingleton.java:25)
at rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.getInstance(VolleySingleton.java:44)
at rides.even.odd.oddorevenrides.MyApplication.instantiateVolleyQueue(MyApplication.java:35)
at rides.even.odd.oddorevenrides.MyApplication.onCreate(MyApplication.java:31)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I am fairly new to java and android and am unable to figure out why is it not able find the class. As far as I understand, It is able to find the class but the exception occurs inside the constructor where I am trying to define mImageLoader
. I am not sure though. Any help would be really appreciated.
update
Ok, I tried launching the application with another device (Samsung s4) and the application works fine. But when I try it with another device (Samsung Google Nexus S) it crashes with the above stacktrace.
update #2
I missed some log entries in the above stacktrace, Maybe this can help figure out whats going on.
01-05 05:46:12.933 3029-3029/rides.even.odd.oddorevenrides E/dalvikvm﹕ Could not find class 'rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton$1', referenced from method rides.even.odd.oddorevenrides.volleyclasses.VolleySingleton.<init>
01-05 05:46:12.933 3029-3029/rides.even.odd.oddorevenrides W/dalvikvm﹕ VFY: unable to resolve new-instance 9625 (Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton$1;) in Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton;
01-05 05:46:12.933 3029-3029/rides.even.odd.oddorevenrides D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x001d
01-05 05:46:12.949 3029-3029/rides.even.odd.oddorevenrides D/dalvikvm﹕ DexOpt: unable to opt direct call 0xfd9f at 0x1f in Lrides/even/odd/oddorevenrides/volleyclasses/VolleySingleton;.<init>
01-05 05:46:13.343 3029-3029/rides.even.odd.oddorevenrides D/AndroidRuntime﹕ Shutting down VM
01-05 05:46:13.343 3029-3029/rides.even.odd.oddorevenrides W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d2a300)
01-05 05:46:14.070 3029-3029/rides.even.odd.oddorevenrides E/AndroidRuntime﹕ FATAL EXCEPTION: main