【问题标题】:Can't use both universal image loader and volley image loader inside application class不能在应用程序类中同时使用通用图像加载器和凌空图像加载器
【发布时间】:2016-09-26 07:24:59
【问题描述】:

下面我发布了我的应用程序类。我尝试将universal image loader code 下面的这个添加到Appcontroller.java 类中。

// UNIVERSAL IMAGE LOADER SETUP
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

        // END - UNIVERSAL IMAGE LOADER SETUP

但我无法添加这个。因为在 volley 图像加载器中导入与通用图像加载器不同。

进口:

Volley ImageLoader - 导入 com.android.volley.toolbox.ImageLoader;

通用 ImageLoader -import com.nostra13.universalimageloader.core.ImageLoader;

如果我在 Appcontroller 中添加 Universal Image Loader,我会在这一行 ImageLoader.getInstance().init(config); 中收到编译错误。

因为两个 ImageLoader 导入是不同的。

AppController .java:(应用程序类)

import com.android.volley.toolbox.ImageLoader;

public class AppController extends Application {

    private static int pendingNotificationsCount = 0;

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    LruBitmapCache mLruBitmapCache;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();

        MultiDex.install(this);

        mInstance = this;




    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            getLruBitmapCache();
            mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
        }

        return this.mImageLoader;
    }

    public LruBitmapCache getLruBitmapCache() {
        if (mLruBitmapCache == null)
            mLruBitmapCache = new LruBitmapCache();
        return this.mLruBitmapCache;
    }

    public void addToRequestQueue(Request<String> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        req.setShouldCache(false);
        getRequestQueue().add(req);
    }

    public void addToRequestQueue(JsonObjectRequest req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }


    public static int getPendingNotificationsCount() {
        return pendingNotificationsCount;
    }

    public static void setPendingNotificationsCount(int pendingNotifications) {
        pendingNotificationsCount = pendingNotifications;
    }

}

【问题讨论】:

  • 你为什么不能?你怎么不说?
  • @greenapps 编辑了帖子。请检查
  • I am getting compile error 有什么理由不提及确切的错误吗?你让我们猜猜。为什么?

标签: android


【解决方案1】:

您的问题并非来自同时使用 UIL 和 Volley,而是来自不同库的 2 个具有相同名称的类。

您可以使用像com.package.path.ImageLoader 这样的完整限定名来代替import 来解决问题。

你也可以看看这个问题:Importing two classes with same name. How to handle?

使用演示代码更新

代替

    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

    ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

    // END - UNIVERSAL IMAGE LOADER SETUP

删除ImageLoaderimport,并将代码更改为:

    // UNIVERSAL IMAGE LOADER SETUP
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .discCacheSize(100 * 1024 * 1024).build();

            com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);      ------> Compile error occurred here.because of imageloader import.

    // END - UNIVERSAL IMAGE LOADER SETUP

【讨论】:

    【解决方案2】:

    您可以删除导入并使用com.android.volley.toolbox.ImageLoadercom.nostra13.universalimageloader.core.ImageLoader,而不是代码中的ImageLoader。

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 2012-08-19
      • 2017-04-07
      • 1970-01-01
      • 2016-06-23
      相关资源
      最近更新 更多