इसी प्रकार के प्रश्न asked herecode that gave from this error किया गया है, here और here लेकिन संदर्भ इस से काफी अलग है और इसके अलावा एंड्रॉइड और एंड्रॉइड स्टूडियो के निर्माताओं द्वारा लिखा गया है।"चेतावनी: स्थिर क्षेत्रों में एंड्रॉयड संदर्भ कक्षाएं न रखें, यह एक स्मृति रिसाव (टूट जाता है त्वरित रन है और यह भी) कि"
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...
करने के लिए त्रुटि disappers बदल लेकिन एक और समस्या आ जाती है ।
मैं रीसाइक्लिंग व्यू में MySingleton
का उपयोग करता हूं। तो इस लाइन
@Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) { ImageLoader imageLoader = MySingleton.getInstance(mContext).getImageLoader();
मुझे
गैर स्थिर विधि 'getInstance (android.content.Context)' एक स्थिर संदर्भ से refrenced नहीं किया जा सकता बताता है।
कृपया किसी को भी इसे ठीक करने जानता है?
यह आपके द्वारा लिंक किए गए अन्य प्रश्नों से अलग कैसे है? – nhouser9
कोड एंट यह एप्लिकेशन – X09
https://stackoverflow.com/questions/39840818/android-googles-contradiction-on-singleton-pattern/39841446#39841446 – CommonsWare