"警告:不要在静态字段中放置 Android 上下文类;这是内存泄漏(也会破坏 Instant Run)" [英] "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

查看:22
本文介绍了"警告:不要在静态字段中放置 Android 上下文类;这是内存泄漏(也会破坏 Instant Run)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题是 在这里询问此处此处 但上下文与此完全不同,而且 由此错误产生的代码 是由 Android 和 Android Studio 的制造商编写的.

Similar question have been asked here, here and here but the context is quite different from this and moreover the code that gave from this error is written by the makers of Android and 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... 更改为 public synchronized MySingleton getInstance(Context... 错误消失,但出现另一个问题.

Now if I remove the static keyword, change public static synchronized MySingleton getInstance(Context... to public synchronized MySingleton getInstance(Context... the error disappers but another problem comes up.

我在 RecyclerView 中使用 MySingleton.所以这一行

I use MySingleton in RecyclerView. So this line

@Overridepublic void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {ImageLoader imageLoader = MySingleton.getInstance(mContext).getImageLoader();

告诉我

非静态方法getInstance(android.content.Context)"不能从静态上下文中引用.

Non-static method 'getInstance(android.content.Context)' cannot be refrenced from a static context.

请问有人知道如何解决这个问题吗?

Please anybody knows how to fix this?

推荐答案

我在 对类似问题的回答中找到了解决方案由 CommonsWare 回答

我引用

引用的 Lint 警告并不是在抱怨创建单例.它抱怨创建持有对一个引用的单身人士任意上下文,因为它可能类似于活动.希望通过将 mContext = context 更改为 mContext =context.getApplicationContext(),你将摆脱那个警告(尽管这可能仍然会破坏 Instant Run——我不能真的要评论一下).

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).

因此,Google 实际上并未与自己签约.为了解决这个问题,如果将 this.getApplicationContext 作为上下文的参数提供,则不会有内存泄漏.

So Google is not actually contracting itself. To fix this, if this.getApplicationContext is supplied as a parameter for the context, then there will be no memory leak.

所以本质上,忽略警告并提供 this.getApplicationContext 作为上下文的参数.

So in essence, ignore the warning and supply this.getApplicationContext as a parameter for the context.

这篇关于&quot;警告:不要在静态字段中放置 Android 上下文类;这是内存泄漏(也会破坏 Instant Run)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆