我之前问过一个类似的问题,但我不明白该怎么做,我还阅读了其他解决方案,例如:什么是 NullPointerException,以及如何修复它?我还是不知道该怎么办,请帮忙:
据我所知,我context = null;
和我不知道为什么,以及如何解决它......
我编写了一个 UniversImageLoader.class 以便能够通过多个活动加载图像。现在我已经在我的所有活动中启动了它,但是在我的 UIL 类中,我需要传递一个上下文。
public class UniversalImageLoader {
private static final int defaultImage = R.drawable.ic_android;
private Context mContext;
public UniversalImageLoader(Context context) {
mContext = context;
}
public ImageLoaderConfiguration getConfig(){
//File cacheDir = StorageUtils.getCacheDirectory(mContext);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(mContext)//<--the error is in this line
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, null)
.threadPriority(Thread.NORM_PRIORITY - 2) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(mContext)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
return config;
}
在 HomeActivity 中:[在每个 Activity 中我都这样称呼它]
private void initImageLoader(){
UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);
ImageLoader.getInstance().init(universalImageLoader.getConfig());
}
在所有这些活动中,我在 OnCreate 方法中调用它,如下所示:
initImageLoader();
所以我已经阅读了,查看了其他解决方案,但找不到可以理解的答案......您的指导将不胜感激!
this.mContext = context;
在构造函数中使用,以便将您的上下文传递给当前类的上下文。
并通过getApplicationContext
或yourActivityName.this
您在何处使用此类。
如果您在片段中使用它,请使用getActivity
或getContext
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句