Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从多个类访问共享首选项_Java_Android_Sharedpreferences - Fatal编程技术网

Java 从多个类访问共享首选项

Java 从多个类访问共享首选项,java,android,sharedpreferences,Java,Android,Sharedpreferences,我需要能够通过我的应用程序从许多不同的类访问共享优先对象,这些类扩展了一系列类型 目前,我一直在通过在应用程序的启动活动中创建一个静态变量来实现这一点 ... public static SharedPreferences sharedpreferences; SharedPreferences.Editor editor; public void onCreate(Bundle savedInstanceState) {

我需要能够通过我的应用程序从许多不同的类访问共享优先对象,这些类扩展了一系列类型

目前,我一直在通过在应用程序的启动活动中创建一个静态变量来实现这一点

...
        public static SharedPreferences sharedpreferences;
        SharedPreferences.Editor editor;

        public void onCreate(Bundle savedInstanceState) {
            sharedpreferences = getSharedPreferences("PrefFile", MODE_PRIVATE); 
            editor = sharedpreferences.edit();
                ...
        }
...
然后从另一个类中,我通过:
StartActivity.SharedReferences

但是,在大多数情况下,这可以正常工作,但是如果应用程序仍在后台运行,并且用户返回应用程序,使其返回到上一个活动,而不重新运行开始活动,
StartActivity.SharedReferences
现在为空,因此如果我尝试访问它,将引发NullPointerExecOption


我如何允许多个类访问同一个共享首选项变量而不使其变为Null

创建一个它的单例实例,该实例将在第一个get方法上初始化

private static class SingletonHolder {
    private static SharedPreferences INSTANCE = getSharedPreferences("PrefFile", MODE_PRIVATE); 
}

public static SharedPreferences getSharedPreferences() {
    return SingletonHolder.INSTANCE;
} 

创建如下所示的单例类:

    public class AppPreferences {
        private SharedPreferences sPreferences;
        public static void init(Context context) {
            sPreferences = context.getSharedPreferences(PREFERENCES_NAME, 0);
        }

        public static SharedPreferences getPrefs() {
            return sPreferences;
        }
    }
然后创建自定义应用程序子类:

    public class App extends Application {

        @Override
        public void onCreate() {
            super.onCreate();
            AppPreferences.init(this);
        }
    }
并将其添加到您的AndroidManifest.xml中:

    <application
        android:name="com.example.App"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    ...

...