1

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)

1 Answers1

0

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()));

webzooh
  • 356
  • 2
  • 9
  • I can't setImage (method) is static, and createDefault in ImageLoaderConfiguration cannot be applied – Ilir Selmani Mar 27 '18 at 18:04
  • Do you use `public UniversalImageLoader(Context context)` and `public ImageLoaderConfiguration getConfig()` at all? – webzooh Mar 27 '18 at 18:16
  • I forgot to copy that to another activity: private void initImageLoader(){ UniversalImageLoader imageLoader = new UniversalImageLoader(MainActivity.this); ImageLoader.getInstance().init(imageLoader.getConfig()); } SOLVED thanks :D – Ilir Selmani Mar 27 '18 at 18:25
  • Im glad that I helped you :) Please accept my answer. – webzooh Mar 27 '18 at 18:28