gpt4 book ai didi

java - Android - Universal ImageLoader错误必须在使用前进行配置初始化

转载 作者:太空宇宙 更新时间:2023-11-04 10:35:37 24 4
gpt4 key购买 nike

错误:

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

我正在努力以将 GridView 放入 fragment 中的方法来显示图像。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 的 fragment (方法):

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

日志猫:

 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)

最佳答案

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

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

关于java - Android - Universal ImageLoader错误必须在使用前进行配置初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49519000/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com