“警告:请勿将Android上下文类放在静态字段中;这是内存泄漏(并且还会中断即时运行)". [英] "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

查看:121
本文介绍了“警告:请勿将Android上下文类放在静态字段中;这是内存泄漏(并且还会中断即时运行)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题是此处代码由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

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

@Override public 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(),您将摆脱该警告 (尽管有可能仍然会中断即时运行-我不能 真的对此发表评论.

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.

这篇关于“警告:请勿将Android上下文类放在静态字段中;这是内存泄漏(并且还会中断即时运行)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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