为什么得到此"Context = NullPointerException"?我的作业有误吗? [英] Why do I get this "Context = NullPointerException" error in my homework?

查看:93
本文介绍了为什么得到此"Context = NullPointerException"?我的作业有误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个家庭作业教程,该教程将构建一个Instagram应用程序.该教程大约有两年的历史了,我在编码方面遇到了一些问题.

I am doing a tutorial for homework, which is to build an Instagram app. The tutorial is about two years old and I am having some problems with the coding.

我遇到以下错误,不确定为什么.

I am having the following error and am not sure why.

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference

我的UniversalImageLoader类

My UniversalImageLoader class

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中]

in HomeActivity:(and OnCreate)[in every Activity I call it like this]

initImageLoader();


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

推荐答案

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

When you are creating object of UniversalImageLoader class, pass getApplicationContext() instead of activity context.

应用程序上下文可通过整个应用程序使用,而活动上下文则绑定到活动生命周期.

Application context is available through out application while activity context is bound to activity life cycle.

更新:

应用程序上下文:这是一个单例实例,可以通过getApplicationContext()在活动中对其进行访问.该上下文与应用程序的生命周期相关.当您需要一个生命周期不同于当前上下文的上下文时,或者当您传递超出活动范围的上下文时,可以使用应用程序上下文

Application Context: It is an instance which is the singleton and can be accessed in an activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of an activity

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

活动上下文.此上下文在活动中可用.该上下文与活动的生命周期相关.

Activity Context This context is available in an activity. This context is tied to the lifecycle of an activity.

在这里阅读更多有关Activity上下文和应用程序上下文的区别的信息. https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

Here read more about the difference in Activity context and application context. https://blog.mindorks.com/understanding-context-in-android-application-330913e32514

对于多个活动,您可以在Application类的onCreate方法中进行初始化.

For Multiple Activities you can initialize in Application class onCreate method.

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


}

然后在活动"中获取像这样的图像加载器实例.

Then in your Activity get image Loader instance like this.

ImageLoader mImageLoader = ImageLoader.getInstance();

还需要像这样在AndroidManifest中添加Application类.

Also you need to add your Application class in AndroidManifest like this.

<application
    android:name=".MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"

这篇关于为什么得到此"Context = NullPointerException"?我的作业有误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆