【发布时间】:2019-10-17 20:35:35
【问题描述】:
我正在做一个家庭作业教程,即构建一个 Instagram 应用程序。该教程大约有两年的历史,我在编码方面遇到了一些问题。
我遇到以下错误,不知道为什么。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我的 UniversalImageLoader 类
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 中:(和 OnCreate)[在我这样称呼的每个 Activity 中]
initImageLoader();
private void initImageLoader(){
UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);
ImageLoader.getInstance().init(universalImageLoader.getConfig());
}
【问题讨论】:
-
分享方法getConfig()的调用。您是否初始化了通用图像加载器 sdk?这是供参考的设置指南github.com/nostra13/Android-Universal-Image-Loader/wiki/…
-
显然,
mContext是null。这意味着您将null传递给了UniversalImageLoader类。 -
@TheAnkush...在哪里?我必须打电话吗?根据我需要在 MAinActivity.class 中执行的指南...我现在调用它它无法打开 Activity...
-
@CommonsWare...这听起来很愚蠢,但我需要传递给它什么?
-
你需要传递一个
Context。由于这似乎正在加载图像,因此您的Activity似乎很可能是Context。
标签: java android universal-image-loader