Android Volley.newRequestQueue正在导致OutOfMemoryError
我正在使用截击设置图像url。我的代码在以下位置崩溃:Android Volley.newRequestQueue正在导致OutOfMemoryError,android,caching,out-of-memory,android-volley,Android,Caching,Out Of Memory,Android Volley,我正在使用截击设置图像url。我的代码在以下位置崩溃: mrq=Volley.newRequestQueue(this); Log cat表示豁免位于:com.android.volley.toolbox.DiskBasedCache.streamToBytes 如果我注释掉代码,程序不会崩溃 我试过几次重启我的手机,因为在我的研究中,我发现这对一些有这个问题的人是有效的 为什么创建RequestQueue要使用这么多内存 如何防止OutOfMemoryError发生 我需要清空缓存吗 感谢您
mrq=Volley.newRequestQueue(this);
Log cat表示豁免位于:com.android.volley.toolbox.DiskBasedCache.streamToBytes
如果我注释掉代码,程序不会崩溃
我试过几次重启我的手机,因为在我的研究中,我发现这对一些有这个问题的人是有效的
为什么创建RequestQueue要使用这么多内存
如何防止OutOfMemoryError发生
我需要清空缓存吗
感谢您的帮助,并花时间阅读此文章。如何初始化您的请求队列?我怀疑您正在为每个活动创建RequestQueues。因此,请在应用程序中启动它
public class ApplicationController extends Application {
private static ApplicationController sInstance;
private RequestQueue mRequestQueue;
@Override
public void onCreate() {
super.onCreate();
// initialize the singleton
sInstance = this;
}
public static synchronized ApplicationController getInstance() {
return sInstance;
}
public RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be
// created when it is accessed for the first time
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
//your code
}
并从活动中获取请求队列作为
mrq = ApplicationController.getInstance().getRequestQueue();
如何初始化RequestQueue?我怀疑您正在为每个活动创建RequestQueues。因此,请在应用程序中启动它
public class ApplicationController extends Application {
private static ApplicationController sInstance;
private RequestQueue mRequestQueue;
@Override
public void onCreate() {
super.onCreate();
// initialize the singleton
sInstance = this;
}
public static synchronized ApplicationController getInstance() {
return sInstance;
}
public RequestQueue getRequestQueue() {
// lazy initialize the request queue, the queue instance will be
// created when it is accessed for the first time
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
//your code
}
并从活动中获取请求队列作为
mrq = ApplicationController.getInstance().getRequestQueue();
一个关键概念是,必须使用应用程序上下文而不是活动上下文实例化RequestQueue。这确保了RequestQueue将在应用程序的整个生命周期内持续,而不是每次重新创建活动时都重新创建。一个关键概念是,RequestQueue必须使用应用程序上下文而不是活动上下文实例化。这确保了RequestQueue将在应用程序的整个生命周期内持续,而不是每次重新创建活动时都重新创建它您是否在活动中启动mrq,即RequestQueue?我是<代码>私有请求队列mrq代码>您是否在活动中启动mrq即RequestQueue?我是<代码>私有请求队列mrq你的怀疑是对的。我知道这会引起什么问题。我已经实现了您建议的功能,我得到了相同的异常。请清除缓存并重新运行应用程序,或者只是尝试重新安装应用程序,您是否将请求作为mrq.add(req)添加到队列中@user3594241您是否使用ImageLoaderYes设置图像,我使用ImageLoader。@lock很乐意帮忙。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受您的怀疑是正确的。我知道这会引起什么问题。我已经实现了您建议的功能,我得到了相同的异常。请清除缓存并重新运行应用程序,或者只是尝试重新安装应用程序,您是否将请求作为mrq.add(req)添加到队列中@user3594241您是否使用ImageLoaderYes设置图像,我使用ImageLoader。@lock很乐意帮忙。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受