我正在使用 Volley 设置图像 url。我的代码在以下位置崩溃:
mrq=Volley.newRequestQueue(this);
Log cat 表示豁免位于:com.android.volley.toolbox.DiskBasedCache.streamToBytes
如果我将代码注释掉,程序不会崩溃。
我已经尝试重启我的手机几次,因为在我的研究中我发现这对一些遇到这个问题的人有效。
为什么创建一个 RequestQueue 使用这么多内存?
如何防止发生 OutOfMemoryError?
我需要清空缓存吗?
感谢您的帮助并花时间阅读本文。
最佳答案
如何初始化 RequestQueue?我怀疑您正在为每个 Activity 创建 RequestQueues。因此在您的 Application 类中将其启动为
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
}
然后从您的 Activity 中获取请求队列作为
mrq = ApplicationController.getInstance().getRequestQueue();
关于android - Volley.newRequestQueue 导致 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021411/