简体   繁体   中英

Android - Universal ImageLoader error must be init with configuration before using

Error:

java.lang.IllegalStateException: ImageLoader must be init with configuration before using

I'm struggling to display the images in my approach of putting the gridview inside a fragments. PostListAdapter where i call UniversalImageLoader:

public class ViewHolder extends RecyclerView.ViewHolder{

    ImageView mPostImage;

    public ViewHolder(View itemView) {
        super(itemView);
        mPostImage = (ImageView) itemView.findViewById(R.id.post_image1);

        int gridWidth = mContext.getResources().getDisplayMetrics().widthPixels;
        int imageWidth = gridWidth/NUM_GRID_COLUMNS;
        mPostImage.setMaxHeight(imageWidth);
        mPostImage.setMaxWidth(imageWidth);
    }
}

public PostListAdapter(Context context, ArrayList<Post> posts) {
    mPosts = posts;
    mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_view_post, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    UniversalImageLoader.setImage(mPosts.get(position).getImage(), holder.mPostImage);
}

@Override
public int getItemCount() {
    return mPosts.size();
}

Fragment (method) where i call PostAdapter:

 private void setupPostsList(){
    GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), NUM_GRID_COLUMNS);
    mRecyclerView.setLayoutManager(gridLayoutManager);
    mAdapter = new PostListAdapter(getActivity(), mPosts);
    mRecyclerView.setAdapter(mAdapter);
}

This is UniversalImageLoader class:

public class UniversalImageLoader 

private static final int defaultImage = R.drawable.ic_launcher_background;
private Context mContext;

public UniversalImageLoader(Context context) {
    mContext = context;
}

public ImageLoaderConfiguration getConfig(){
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(defaultImage)
            .showImageForEmptyUri(defaultImage)
            .showImageOnFail(defaultImage)
            .considerExifParams(true)
            .cacheOnDisk(true).cacheInMemory(true)
            .cacheOnDisk(true).resetViewBeforeLoading(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(mContext)
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .diskCacheSize(100 * 1024 * 1024).build();

    return configuration;
}

public static void setImage(String imgURL, ImageView image){

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(imgURL, image);
}

Logcat:

 java.lang.IllegalStateException: ImageLoader must be init with configuration before using
                                                                          at com.test.test.test.util.UniversalImageLoader.setImage(UniversalImageLoader.java:54)
                                                                          at com.test.test.test.util.PostListAdapter.onBindViewHolder(PostListAdapter.java:59)
                                                                          at com.test.test.test.util.PostListAdapter.onBindViewHolder(PostListAdapter.java:23)

You need to call init(conf) method before using displayImage() .

You are not using getConfig() method so you should probably add in UniversalImageLoader constructor imageLoader.init(ImageLoaderConfiguration.createDefault(getConfig()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM