使用ImageLoader时出错,说它未初始化

时间:2015-03-19 08:04:58

标签: java android adapter image-loading

我关注this tutorial。我正在尝试创建一个简单的图像视图寻呼机。本教程后的示例项目工作正常。但是,当我尝试在我的项目中使用相同的代码时,我得到的ImageLoader没有初始化错误。

详细的logcat

03-19 13:30:02.290: E/AndroidRuntime(7716): FATAL EXCEPTION: main
03-19 13:30:02.290: E/AndroidRuntime(7716): java.lang.IllegalStateException: ImageLoader must be init with configuration before using
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.nostra13.universalimageloader.core.ImageLoader.checkConfiguration(ImageLoader.java:340)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:179)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.specialspots.specialspots.ImageSlideAdapter.instantiateItem(ImageSlideAdapter.java:84)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.specialspots.specialspots.ImageSlideAdapter.instantiateItem(ImageSlideAdapter.java:1)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:442)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.specialspots.specialspots.HomeFragment$RequestImgTask.onPostExecute(HomeFragment.java:278)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.specialspots.specialspots.HomeFragment$RequestImgTask.onPostExecute(HomeFragment.java:1)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.os.Looper.loop(Looper.java:176)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at android.app.ActivityThread.main(ActivityThread.java:5419)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at java.lang.reflect.Method.invoke(Method.java:525)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
03-19 13:30:02.290: E/AndroidRuntime(7716):     at dalvik.system.NativeStart.main(Native Method)

我无法解释为什么它不会被初始化但是相同的代码在示例应用程序中正常工作。

负责错误的适配器:

public class ImageSlideAdapter extends PagerAdapter {
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private ImageLoadingListener imageListener;
    FragmentActivity activity;
    List<Product> products;
    HomeFragment homeFragment;

    public ImageSlideAdapter(FragmentActivity activity, List<Product> products,
            HomeFragment homeFragment) {
        this.activity = activity;
        this.homeFragment = homeFragment;
        this.products = products;
        options = new DisplayImageOptions.Builder()
                .showImageOnFail(R.drawable.ic_error)
                .showStubImage(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.ic_empty).cacheInMemory()
                .cacheOnDisc().build();

        imageListener = new ImageDisplayListener();
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.vp_image, container, false);

        ImageView mImageView = (ImageView) view
                .findViewById(R.id.image_display);
        mImageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bundle arguments = new Bundle();
                Fragment fragment = null;
                Log.d("position adapter", "" + position);
                Product product = (Product) products.get(position);
                arguments.putParcelable("singleProduct", product);

                // Start a new fragment
                fragment = new ProductDetailFragment();
                fragment.setArguments(arguments);

                FragmentTransaction transaction = activity
                        .getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, fragment,
                        ProductDetailFragment.ARG_ITEM_ID);
                transaction.addToBackStack(ProductDetailFragment.ARG_ITEM_ID);
                transaction.commit();
            }
        });
        imageLoader.displayImage(
                ((Product) products.get(position)).getImageUrl(), mImageView,
                options, imageListener);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    private static class ImageDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}

请告诉我哪里出错了。要做些什么才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您必须在Application类中初始化ImageLoader。

public class MyApplication extends Application {


   public static void initImageLoader(Context context) {

      File cacheDirectory = StorageUtils.getOwnCacheDirectory(context, ApplicationConstant.IMAGE_CACHE_DIRECTORY);
      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)
                                                                                     .threadPoolSize(5)
                                                                                     .denyCacheImageMultipleSizesInMemory()
                                                                                     .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                                                                                     .memoryCacheSize(50 * 1024 * 1024)
                                                                                     .diskCache(new UnlimitedDiscCache(cacheDirectory))
                                                                                     .tasksProcessingOrder(QueueProcessingType.LIFO)
                                                                                     .writeDebugLogs()
                                                                                     .build();
      // Initialize ImageLoader with configuration.
      ImageLoader.getInstance().init(config);

   }

   @Override
   public void onCreate() {
      super.onCreate();
      initImageLoader(getApplicationContext());
   }
}

必须在应用程序标记

中的清单中设置应用程序类
<application
        android:name="MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
</application>