为什么我的作业中会出现“Context = NullPointerException”错误?

我正在做作业教程,即构建 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());

    }


暮色呼如
浏览 120回答 1
1回答

慕侠2389804

当您创建 UniversalImageLoader 类的对象时,传递getApplicationContext()而不是活动上下文。应用程序上下文在整个应用程序中可用,而活动上下文则绑定到活动生命周期。更新:Application Context:它是一个单例实例,可以通过 getApplicationContext() 在活动中访问。此上下文与应用程序的生命周期相关联。应用程序上下文可用于您需要其生命周期与当前上下文分离的上下文,或者当您传递超出活动范围的上下文时private void initImageLoader(){&nbsp; &nbsp; UniversalImageLoader universalImageLoader = new UniversalImageLoader(getApplicationContext());&nbsp; &nbsp; ImageLoader.getInstance().init(universalImageLoader.getConfig());}活动上下文此上下文在活动中可用。此上下文与活动的生命周期相关联。在这里阅读更多关于 Activity context 和 application context 的区别。 https://blog.mindorks.com/understanding-context-in-android-application-330913e32514对于多个活动,您可以在 Application 类的 onCreate 方法中进行初始化。public class MyApplication extends Application {@Overridepublic void onCreate() {&nbsp; &nbsp; super.onCreate();&nbsp; &nbsp; &nbsp; &nbsp; // Initialize the Universal Image Loader here&nbsp; &nbsp; DisplayImageOptions defaultOptions = new&nbsp;&nbsp; &nbsp; DisplayImageOptions.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .cacheOnDisk(true).cacheInMemory(true).build();&nbsp; &nbsp; ImageLoaderConfiguration config = new&nbsp;&nbsp; &nbsp; ImageLoaderConfiguration.Builder(getApplicationContext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .defaultDisplayImageOptions(defaultOptions).build();&nbsp; &nbsp; ImageLoader.getInstance().init(config);}然后在您的 Activity 中像这样获取图像加载器实例。ImageLoader mImageLoader = ImageLoader.getInstance();您还需要像这样在 AndroidManifest 中添加您的应用程序类。<application&nbsp; &nbsp; android:name=".MyApplication"&nbsp; &nbsp; android:icon="@mipmap/ic_launcher"&nbsp; &nbsp; android:label="@string/app_name"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java