第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么我的作業(yè)中會(huì)出現(xiàn)“Context = NullPointerException”錯(cuò)誤?

為什么我的作業(yè)中會(huì)出現(xiàn)“Context = NullPointerException”錯(cuò)誤?

暮色呼如 2022-12-28 14:18:42
我正在做作業(yè)教程,即構(gòu)建 Instagram 應(yīng)用程序。該教程大約有兩年的歷史,我在編碼方面遇到了一些問(wèn)題。我有以下錯(cuò)誤,我不確定為什么。 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)[在每個(gè) Activity 中我都這樣稱呼它]initImageLoader();private void initImageLoader(){        UniversalImageLoader universalImageLoader = new UniversalImageLoader(mContext);        ImageLoader.getInstance().init(universalImageLoader.getConfig());    }
查看完整描述

1 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

當(dāng)您創(chuàng)建 UniversalImageLoader 類的對(duì)象時(shí),傳遞getApplicationContext()而不是活動(dòng)上下文。


應(yīng)用程序上下文在整個(gè)應(yīng)用程序中可用,而活動(dòng)上下文則綁定到活動(dòng)生命周期。


更新:


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


private void initImageLoader(){

    UniversalImageLoader universalImageLoader = new UniversalImageLoader(getApplicationContext());

    ImageLoader.getInstance().init(universalImageLoader.getConfig());

}

活動(dòng)上下文此上下文在活動(dòng)中可用。此上下文與活動(dòng)的生命周期相關(guān)聯(lián)。


在這里閱讀更多關(guān)于 Activity context 和 application context 的區(qū)別。 https://blog.mindorks.com/understanding-context-in-android-application-330913e32514


對(duì)于多個(gè)活動(dòng),您可以在 Application 類的 onCreate 方法中進(jìn)行初始化。


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 中像這樣獲取圖像加載器實(shí)例。


ImageLoader mImageLoader = ImageLoader.getInstance();

您還需要像這樣在 AndroidManifest 中添加您的應(yīng)用程序類。


<application

    android:name=".MyApplication"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"


查看完整回答
反對(duì) 回復(fù) 2022-12-28
  • 1 回答
  • 0 關(guān)注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)