Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我会得到这个;上下文=NullPointerException“;我的作业有错误吗?_Java_Android_Universal Image Loader - Fatal编程技术网

Java 为什么我会得到这个;上下文=NullPointerException“;我的作业有错误吗?

Java 为什么我会得到这个;上下文=NullPointerException“;我的作业有错误吗?,java,android,universal-image-loader,Java,Android,Universal Image Loader,我正在做一个家庭作业教程,这是建立一个Instagram应用程序。这个教程已经有两年了,我在编码方面遇到了一些问题 我有以下错误,不确定原因 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 我的Universal

我正在做一个家庭作业教程,这是建立一个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;
    }

创建UniversalImageLoader类的对象时,传递getApplicationContext(),而不是活动上下文

应用程序上下文在整个应用程序中可用,而活动上下文绑定到活动生命周期

更新:

应用程序上下文:它是一个单例实例,可以通过getApplicationContext()在活动中访问。此上下文与应用程序的生命周期相关联。应用程序上下文可用于需要其生命周期与当前上下文分开的上下文的地方,或在传递超出活动范围的上下文时使用

private void initImageLoader(){
    UniversalImageLoader universalImageLoader = new UniversalImageLoader(getApplicationContext());
    ImageLoader.getInstance().init(universalImageLoader.getConfig());
}
活动上下文此上下文在活动中可用。此上下文与活动的生命周期相关联

这里阅读更多关于活动上下文和应用程序上下文的区别。

对于多个活动,可以在应用程序类onCreate方法中初始化

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
        // Initialize the Universal Image Loader here

    DisplayImageOptions defaultOptions = new 
    DisplayImageOptions.Builder()
            .cacheOnDisk(true).cacheInMemory(true).build();

    ImageLoaderConfiguration config = new 
    ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions).build();

    ImageLoader.getInstance().init(config);


}
然后在您的活动中获得如下图像加载程序实例

ImageLoader mImageLoader = ImageLoader.getInstance();
<application
    android:name=".MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
您还需要像这样在AndroidManifest中添加应用程序类

ImageLoader mImageLoader = ImageLoader.getInstance();
<application
    android:name=".MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"

共享方法getConfig()的调用。您是否初始化了Universal image loader sdk?这是供参考的设置指南,
mContext
null
。这意味着您将
null
传递给
UniversalImageLoader
类。@ankush。。。哪里我必须叫它吗?根据指南,我需要在MAinActivity.课堂上做。。。我现在叫它,它无法打开活动…@commonware。。。这听起来很愚蠢,但我需要传递什么呢?您需要传递
上下文。由于这似乎是在加载图像,因此您的
活动
可能是一个
上下文
。感谢您提供的信息…这没有任何意义。。。请你解释一下,使用一个示例代码片段…@BenVanJaarsveld-我已经用代码更新了我的答案。观察我是如何初始化通用映像加载程序的。您共享的代码段,mContext的值不清楚。我相信您可能已经设置了mContext=。这