0
我在我的片段之一使用PreferenceManager使用這樣的代碼。試圖調用虛擬方法'java.lang.String android.content.Context.getPackageName()
String userId = PreferenceManager.getDefaultSharedPreferences(getContext()).getString("userId", "");
它引起一些時間錯誤稱爲上面。我已經在stackflow中搜索了很多問題,但無法修復它。我完全錯誤日誌就像下面
FATAL EXCEPTION: main
Process: com.example.shayari, PID: 29366
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.support.v7.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:308)
at android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:303)
at com.example.shayari.material.QuotesListFragment.updateQuotesListServer(QuotesListFragment.java:252)
at com.example.shayari.material.QuotesListFragment.access$900(QuotesListFragment.java:45)
at com.example.shayari.material.QuotesListFragment$4.onResponse(QuotesListFragment.java:202)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
任何人都面臨着類似的問題,如果找到任何解決辦法,讓我知道。非常感謝