亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的作業中會出現“Context = NullPointerException”錯誤?

為什么我的作業中會出現“Context = NullPointerException”錯誤?

暮色呼如 2022-12-28 14:18:42
我正在做作業教程,即構建 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());    }
查看完整描述

1 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

當您創建 UniversalImageLoader 類的對象時,傳遞getApplicationContext()而不是活動上下文。


應用程序上下文在整個應用程序中可用,而活動上下文則綁定到活動生命周期。


更新:


Application Context:它是一個單例實例,可以通過 getApplicationContext() 在活動中訪問。此上下文與應用程序的生命周期相關聯。應用程序上下文可用于您需要其生命周期與當前上下文分離的上下文,或者當您傳遞超出活動范圍的上下文時


private void initImageLoader(){

    UniversalImageLoader universalImageLoader = new UniversalImageLoader(getApplicationContext());

    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 {


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



}

然后在您的 Activity 中像這樣獲取圖像加載器實例。


ImageLoader mImageLoader = ImageLoader.getInstance();

您還需要像這樣在 AndroidManifest 中添加您的應用程序類。


<application

    android:name=".MyApplication"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"


查看完整回答
反對 回復 2022-12-28
  • 1 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號