修复 Universal Image Loader 的警告:"Try to initialize ImageLoader which had already been initialized before. Universal image loader"

Fix warning of Universal Image Loader: "Try to initialize ImageLoader which had already been initialized before. Universal image loader"

每当我在我的应用程序中加载图像时,我的 logcat 都会给我这条消息。

04-09 19:09:59.241: W/ImageLoader(276): 
Try to initialize ImageLoader which had already been initialized before. 
To re-init ImageLoader with new configuration call ImageLoader.destroy() at first.

我不明白为什么。这是我的代码:

//If save on disk setting is false, do not save on disk. else, save on disk.
    if (dataReturned =="false"){
        defaultOptions = new DisplayImageOptions.Builder()
        .cacheInMemory(true)
        .cacheOnDisk(false)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();
    }else{
        defaultOptions = new DisplayImageOptions.Builder()
        .cacheOnDisc(true)
        .cacheInMemory(true)
        .bitmapConfig(Bitmap.Config.RGB_565)
        .build();
    }
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .denyCacheImageMultipleSizesInMemory()
        .build();
ImageLoader.getInstance().init(config); // Do it on Application start

其他人也有同样的问题,但对我没有帮助 Fix a warning of ImageLoader : "Try to initialize ImageLoader which had already been initialized before" 因为我只在 onCreate 上初始化了 ImageLoader,只有一次。而他一遍又一遍的初始化。

我假设你在 Activity 上有它。

如果你要把它放在那里,你必须像这样在 onDestroy 上调用 ImageLoader.destroy:

protected void onDestroy() {
    ImageLoader.getInstance().destroy();
}

您可以将初始化放在您的应用程序中 Class http://developer.android.com/reference/android/app/Application.html 像这样:

YourApplication extends Application {
    protected void onCreate() {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .defaultDisplayImageOptions(defaultOptions)
        .denyCacheImageMultipleSizesInMemory()
        .build();
        ImageLoader.getInstance().init(config);
    }
}