代码之家  ›  专栏  ›  技术社区  ›  MyName

通过静态方法访问SharedReference

  •  30
  • MyName  · 技术社区  · 15 年前

    这给我带来了一些问题,因为显然不可能从静态方法调用方法“getSharedPreferences”。

    Cannot make a static reference to the non-static method 
    getSharedPreferences(String, int) from the type ContextWrapper
    

    我试图通过使用一个活动实例来解决这个问题,比如:

    public static SharedPreferences getSharedPreferences () {
      Activity act = new Activity();
      return act.getSharedPreferences("FILE", 0);
    }
    

    此代码提供了一个空点异常。

    提前谢谢。

    6 回复  |  直到 15 年前
        1
  •  36
  •   Sa Dec Cristian    13 年前

    因为在这种情况下, act getSharedPreferences() 是一种 Context Activity Service 其他类从 ). 所以,你必须做出选择:

    • getApplicationContext().getSharedPreferences("foo", 0);
      
    • 如果方法在活动或其他类型的上下文之外:

      // you have to pass the context to it. In your case:
      // this is inside a public class
      public static SharedPreferences getSharedPreferences (Context ctxt) {
         return ctxt.getSharedPreferences("FILE", 0);
      }
      
      // and, this is in your activity
      YourClass.this.getSharedPreferences(YourClass.this.getApplicationContext());
      
        2
  •  67
  •   mreichelt    15 年前

    克里斯蒂安的回答很好,但如果你想从任何地方都能获得你的共同偏好,正确的方法是:

    1. 创建的子类 Application ,例如。 public class MyApp extends Application { ...
    2. 设置 android:name <application> 标签在AndroidManifest.xml文件指出你的新班级。 android:name="MyApp" (因此该类被Android识别)
    3. 在应用程序实例的onCreate()方法中,保存上下文(例如。 this )到名为 app getApp()
        3
  •  10
  •   Dan Bray    12 年前

    我遇到了一个类似的问题,我通过简单地将当前上下文传递给静态函数来解决它:

    public static void LoadData(Context context)
    {
        SharedPreferences SaveData = context.getSharedPreferences(FILENAME, MODE_PRIVATE);
        Variable = SaveData.getInt("Variable", 0);
        Variable1 = SaveData.getInt("Variable1", 0);
        Variable2 = SaveData.getInt("Variable2", 0);
    }
    

    由于您是从活动外部调用,因此需要保存上下文:

    public static Context context;
    

    在OnCreate中:

    context = this;
    

    将上下文存储为静态变量可能会导致问题,因为当类被销毁时,静态变量也会被销毁。当应用程序中断并且内存不足时,有时会发生这种情况。只要确保在尝试使用上下文之前总是设置上下文,即使设置上下文的类被随机销毁。

        4
  •  6
  •   kerux2222    10 年前

    这里有一个更好的替代方法,可以将共享的首选项存储在静态字段中。

    1. 使用上下文获取共享的首选项并将它们存储在私有变量中。
    2. 创建公共变量以返回检索到的数据。

    例如

    public class UserInfo extends Application{
        private String SAVED_USERID;
        private String SAVED_USERNAME;
    
        public UserInfo(Context context) {
            SharedPreferences prefs = context.getSharedPreferences(FILE, MODE_PRIVATE);
            SAVED_USERNAME = prefs.getString("UserName", null);
            SAVED_USERID = prefs.getString("UserID", null);
        }
    
        public String getSavedUserName() {
            return SAVED_USERNAME;
        }
    
        public String getSavedUserID() {
             return SAVED_USERID;
        }
    }
    

    活动中的用法

       UserInfo user = new UserInfo(this.getApplicationContext());
    
       String SAVED_USERNAME = user.getSavedUserName();
       String SAVED_USERID = user.getSavedUserID();
    
        5
  •  2
  •   Ed Cooper    9 年前

    我也有同样的需求——我的一些偏好需要经常有效地访问。我还认为,从SharedPreferences读取和写入字符串比获取和设置静态变量要慢一些(但很可能是微不足道的)。我还习惯了使用静态字段,只在启动时检索首选项值,并在关闭时保存它们。

    我不喜欢直接保存对SharedPreferences/context的静态引用,但到目前为止,这种解决方法已经足够了。

    1. 创建一个包含所有需要的静态变量的设置类。

    2. 当应用程序初始化时,检索SharedPreferences字段并立即设置所有设置字段(我在MainActivity的onCreate方法末尾调用“loadSharedPrefs()”方法)。

    3. 在SettingsActivity的preferenceChangeListener的初始化中,在Settings类中设置适当的静态字段。(我在SettingsActivity的onPreferenceChange()的开头调用“setpropertesetting(key,value)”方法)。

        6
  •  0
  •   Eric Smith    8 年前
    public static String getPreferenceValue(Context context) {
        SharedPreferences sharedPreferences = 
            PreferenceManager.getDefaultSharedPreferences(context);
        String key = context.getString(R.string.pref_key);
        String defaultVal = context.getString(R.string.pref_default);
        return sharedPreferences.getString(key,defaulVal);
    }