Android - Universal ImageLoader错误必须在使用前配置为init

时间:2018-03-27 17:25:20

标签: java android

错误:

  

java.lang.IllegalStateException:在使用

之前,ImageLoader必须是带配置的init

我正在努力在将gridview放入片段的方法中显示图像。 PostListAdapter,我调用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();
}

我调用PostAdapter的片段(方法):

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

这是UniversalImageLoader类:

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 个答案:

答案 0 :(得分:0)

在使用init(conf)之前,您需要调用displayImage()方法。

您没有使用getConfig()方法,因此您应该添加UniversalImageLoader构造函数 imageLoader.init(ImageLoaderConfiguration.createDefault(getConfig()));