gpt4 book ai didi

在应用程序 onCreate 中初始化类时出现 java.lang.NoClassDefFoundError

转载 作者:行者123 更新时间:2023-11-29 03:01:28 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序 onCreate 方法上实例化一个类,如下所示:

volleyQueueInstance = VolleySingleton.getInstance(getApplicationContext());

下面是我的 VolleySingleton

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;
}
}

但我的应用程序在启动时崩溃,并显示以下堆栈跟踪,表明没有找到类 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)

我是 java 和 android 的新手,无法弄清楚为什么找不到类。据我所知,它能够找到该类,但异常发生在我试图定义 mImageLoader 的构造函数中。不过我不确定。非常感谢任何帮助。

更新

好的,我尝试用另一台设备 (Samsung s4) 启动应用程序,应用程序运行正常。但是当我在另一台设备(Samsung Google Nexus S)上尝试时,它会因上述堆栈跟踪而崩溃。

更新#2

我在上面的堆栈跟踪中遗漏了一些日志条目,也许这可以帮助弄清楚发生了什么。

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

最佳答案

好的,经过数小时的研究,我终于能够解决它。这里的问题是 MultiDex 支持。我的应用程序中有部分 multidex 支持

multiDexEnabled true

在我的 build.gradle 文件中,但我的应用程序正在扩展 Application 类。

刚刚扩展了我的应用程序类 MultiDexApplication类,并在我的应用程序类文件中添加了以下方法。

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}

能够通过研究以下日志条目来解决它。

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>

关于在应用程序 onCreate 中初始化类时出现 java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34601174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com