java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2720字,纯文字阅读大概需要4分钟。
内容图文
![java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“](/upload/InfoBanner/zyjiaocheng/951/aaa6c1c7ac674482a1a14d7a06d55c4c.jpg)
类似的问题一直是asked here,here和here,但背景与此截然不同,而且code that gave from this error是由Android和Android Studio的制造商编写的.
这是代码:
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@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 MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(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;
}
}
发出警告的行是:
private static MySingleton mInstance;
private static Context mCtx;
现在,如果我删除static关键字,请更改public static synchronized MySingleton getInstance(Context … to public synchronized MySingleton getInstance(Context …错误disappers但是出现了另一个问题.
我在RecyclerView中使用MySingleton.所以这一行
@覆盖
public void onBindViewHolder(final RecyclerView.ViewHolder holder,int position){
ImageLoader imageLoader = MySingleton.getInstance(mContext).getImageLoader();
告诉我
Non-static method ‘getInstance(android.content.Context)’ cannot be refrenced from a static context.
请有人知道如何解决这个问题?
解决方法:
我在answer to a similar question answered by CommonsWare找到了解决方案
我引用
The quoted Lint warning is not complaining about creating singletons.
It is complaining about creating singletons holding a reference to an
arbitrary Context, as that could be something like an Activity.
Hopefully, by changing mContext = context to mContext =
context.getApplicationContext(), you will get rid of that warning
(though it is possible that this still breaks Instant Run — I cannot
really comment on that).Creating singletons is fine, so long as you do so very carefully, to
avoid memory leaks (e.g., holding an indefinite static reference to an
Activity).
因此谷歌实际上并没有签约.要解决此问题,如果this.getApplicationContext作为上下文的参数提供,则不会发生内存泄漏.
所以实质上,忽略警告并提供this.getApplicationContext作为上下文的参数.
内容总结
以上是互联网集市为您收集整理的java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“全部内容,希望文章能够帮你解决java – “警告:不要将Android上下文类放在静态字段中;这是内存泄漏(也打破了Instant Run)“所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。