0

I asked a similar question earlier but i don't understand what to do, I also have read other solutions like: What is a NullPointerException, and how do I fix it? I still don't know what to do, please help:

According to what I understand is that my context = null; and I am not sure why, and how to fix it...

I wrote a UniversImageLoader.class to be able to load images over several activities. Now I have initiated it in all my activities, but in my UIL class, I need to pass a context.

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

in HomeActivity:[in every Activity I call it like this]

private void initImageLoader(){
        UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);
        ImageLoader.getInstance().init(universalImageLoader.getConfig());
    }

and in all those activities I call it in the OnCreate method like this:

initImageLoader();

So I have read up, looked at other solutions but can't find an understandable answer...your guidance will be highly appreciated!

1 Answers1

1

Use this.mContext = context; in constructor so it will pass your context to the context of current class.

And pass getApplicationContext or yourActivityName.this where you are using this class.

If you are using it in fragment use getActivity or getContext.

L. F.
  • 16,219
  • 7
  • 33
  • 67