我在我的應用程序中使用SharedPreferences
來存儲登錄到我的應用程序中的最後一個用戶標識,並且我正在嘗試從非活動類中執行此操作,這會導致我在NullPointerException
代碼的第二行:Android獲得共享首選項問題
Context mContext;
SharedPreferences lastUser = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = lastUser.edit();
我試着mContext的每一個可能的變化:
Acticity.this.mContext;
this.mContext.getApplicationContext();
this.mContext;
但他們並沒有解決我的問題。 任何人都可以給我一些解決方案如何擺脫這個問題,以及如何使用靜態方式的上下文,沒有得到NullPointerException
?