如何将应用程序上下文从Singleton类传递给SharedPreferences?我在 onActivityCreated(Bundle savedInstanceState)方法中有一个片段和一个GridView,在项目单击时,我在logcat中得到NullPointerException:
03-30 05:12:54.784: E/AndroidRuntime(1950): FATAL EXCEPTION: main
03-30 05:12:54.784: E/AndroidRuntime(1950): java.lang.NullPointerException
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
03-30 05:12:54.784: E/AndroidRuntime(1950): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
03-30 05:12:54.784: E/AndroidRuntime(1950): at com.example.duaapp.utility.SharedPreferencesSupplication.save(SharedPreferencesSupplication.java:35)
Singleton Class
public class SingletonClass {
public static Context applicationContext;
public static int fontSizeMin = 17;
public static int fontSizeMax = 35;
public static final String keySelVerseFromList = "keySelVerseFromList";
public static final String keyFavVerses = "keyFavVerses";
public static final String keyListOfVerses = "keyListOfVerses";
public static final String keyIsFavSelected = "keyIsFavSelected";
}
SharedPreferences
public class SharedPreferencesSupplication {
public void save(String valueKey, String value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putString(valueKey, value);
edit.commit();
}
public void save(String valueKey, int value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putInt(valueKey, value);
edit.commit();
}
public void save(String valueKey, boolean value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(valueKey, value);
edit.commit();
}
public void save(String valueKey, long value) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SingletonClass.applicationContext);
SharedPreferences.Editor edit = prefs.edit();
edit.putLong(valueKey, value);
edit.commit();
}
}
在gridview_item_Click上,每当调用 new SharedPreferencesSupplication()。save(SingletonClass.keyIsFavSelected,false); 时,应用程序崩溃并在logcat中引发nullpointer异常。我哪里错了?
答案 0 :(得分:6)
从不在Android中使用对Context的静态引用,这会导致重要的内存泄漏,因为Context引用了所有应用程序资源。
大多数Android UI类都有一个对上下文的动态引用,您可以在getActivity()
的片段中,在视图中getContext()
考虑使用这些而不是上下文单例
有关此here
的更多信息答案 1 :(得分:3)
虽然Guillermo Merino关注有效,但单例类中使用的应用程序Context
不应自动被视为内存泄漏。
调用context.getApplicationContext()
无论访问何处或如何访问,都将始终从您的流程中返回相同的实例。这将是"活着"至少与单身人士一样长,因此对其进行引用不应该造成伤害。
也就是说,执行某些操作可能还不够,正如戴夫史密斯在他的blog post中所描述的那样。
由于OP尝试访问默认SharedPreferences
,因此只需使用应用Context
就可以实现,例如:
// Custom Application class.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SingletonClass.INSTANCE.init(getApplicationContext());
}
}
// Singleton.
public enum SingletonClass {
INSTANCE;
private Context applicationContext;
public static int fontSizeMin = 17;
public static int fontSizeMax = 35;
public static final String keySelVerseFromList = "keySelVerseFromList";
public static final String keyFavVerses = "keyFavVerses";
public static final String keyListOfVerses = "keyListOfVerses";
public static final String keyIsFavSelected = "keyIsFavSelected";
// Make sure you only call this once - from MyApplication.
public void init(final Context context) {
applicationContext = context.getApplicationContext();
}
public Context getApplicationContext() {
if (null == applicationContext) {
throw new IllegalStateException("have you called init(context)?");
}
return applicationContext;
}
}
我不相信这会导致任何内存泄漏,但如果我弄错了,请纠正我。