崩溃日志android ImageLoader必须使用universalimageloader

时间:2016-01-25 06:20:38

标签: android universal-image-loader

您好我是否创建了示例视图图像概念通用图像加载器库,但我已经在我的ViewpagesAdapter.class中初始化了这一行
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(MyActivit‌​y.this));

我仍然得到错误:

java.lang.IllegalStateException: ImageLoader must be init with configuration before using
at com.nostra13.universalimageloader.core.ImageLoader.checkConfiguration(ImageLoader.java:613)
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:236)
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:209)
at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:410)
at ****.****.adapter.ViewpagesAdapter.instantiateItem(ViewpagesAdapter.java:73)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1020)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5705)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5705)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:17915)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5705)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2728)
at android.view.View.measure(View.java:17915)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2412)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1414)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1638)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6598)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

给我更好的解决方案?

我的代码行:

      public class ViewpagesAdapter extends PagerAdapter {

// Declare variables
private Activity activity;
private ArrayList<String> filepath;
private LayoutInflater inflater=null;
public ImageLoader imageLoader;
DisplayImageOptions options;


public ViewpagesAdapter(Activity a, ArrayList<String> fpath) {
    activity = a;
    filepath = fpath;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    File cacheDir = StorageUtils.getOwnCacheDirectory(a, "MyFolderCache");
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.image_thumbnail)
            .showImageForEmptyUri(R.drawable.image_thumbnail)
            .showImageOnFail(R.drawable.image_thumbnail)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}

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

@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;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View itemView;
    itemView=inflater.inflate(R.layout.pagesview, container, false);
    PhotoView image = (PhotoView)itemView.findViewById(R.id.image_pages);
    ImageLoader.getInstance().displayImage(filepath.get(position),image, options, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {

        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        }
    }, new ImageLoadingProgressListener() {
        @Override
        public void onProgressUpdate(String imageUri, View view, int current, int total) {
        }
    });

    container.addView(itemView);
    return itemView;
}

2 个答案:

答案 0 :(得分:3)

从日志看起来你没有初始化图像加载器,所以添加以下行
        ImageLoader的= imageLoader.init(ImageLoaderConfiguration.createDefault(getBaseContext()));

答案 1 :(得分:2)

  

首先在Import

中添加这两行
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
  

然后在此

之后添加以下两行

线。 inflater =(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

imageloader = ImageLoader.getInstance();
imageloader.init(ImageLoaderConfiguration.createDefault(context));
  

然后在getView(),

中添加这样的图像
 imageloader.displayImage(Image Path, ImageView);

希望它会有所帮助。