【问题标题】:shared prefrence stored value not working with in android onCreate()共享首选项存储值在 android onCreate() 中不起作用
【发布时间】:2014-10-27 04:13:05
【问题描述】:

在我的应用程序中,我使用共享首选项,它在 onResume() 中工作正常,只是在 onCreate() 中不起作用。有人知道原因吗?在 onCreate 中,共享首选项值始终仅为 null。

这是我的代码

@Override
protected void onResume() {

    setTextValues();
    super.onResume();
    }

private void setTextValues(){       
      txt1.setText(PreferenceConnector.readString(this,PreferenceConnector.MILEAGE, null));
      txt2.setText(PreferenceConnector.readString(this,PreferenceConnector.YEAR, null));
      txt3.setText(PreferenceConnector.readString(this,PreferenceConnector.CAPACITY, null));         
}

   package com.InternetGMBH.Sample.Utilities;

     import com.InternetGMBH.Tample.Activities.WheelActivity;

     import android.content.Context;
      import android.content.SharedPreferences;
     import android.content.SharedPreferences.Editor;

  public class PreferenceConnector{
  public static final String PREF_NAME = "PEOPLE_PREFERENCES";
  public static final int MODE = Context.MODE_PRIVATE;


public static final String MILEAGE= "MILEAGE";
public static final String YEAR= "YEAR";
public static final String CAPACITY= "CAPACITY";



public static void writeBoolean(Context context, String key, boolean value) {
    getEditor(context).putBoolean(key, value).commit();
}

public static boolean readBoolean(Context context, String key, boolean defValue) {
    return getPreferences(context).getBoolean(key, defValue);
}

public static void writeInteger(Context context, String key, int value) {
    getEditor(context).putInt(key, value).commit();

}

public static int readInteger(Context context, String key, int defValue) {
    return getPreferences(context).getInt(key, defValue);
}

public static void writeString(Context context, String key, String value) {
    getEditor(context).putString(key, value).commit();

}

public static String readString(Context context, String key, String defValue) {
    return getPreferences(context).getString(key, defValue);
}

public static void writeFloat(Context context, String key, float value) {
    getEditor(context).putFloat(key, value).commit();
}

public static float readFloat(Context context, String key, float defValue) {
    return getPreferences(context).getFloat(key, defValue);
}

public static void writeLong(Context context, String key, long value) {
    getEditor(context).putLong(key, value).commit();
}

public static long readLong(Context context, String key, long defValue) {
    return getPreferences(context).getLong(key, defValue);
}

public static SharedPreferences getPreferences(Context context) {
    return context.getSharedPreferences(PREF_NAME, MODE);
}



public static Editor getEditor(Context context) {
    return getPreferences(context).edit();
}

}

如果我在 onCreate() 中调用 setTextValues(),它只显示空值。但 onResume 工作正常。

【问题讨论】:

  • PreferenceConnector 是什么?是你写的东西吗?如果是,你能显示代码吗?
  • onCreate 实现在哪里?

标签: android sharedpreferences


【解决方案1】:

您的 Activity 似乎是您应用程序的第一个 Activity,在 onCreate 方法中 SharedPreferences 可能未正确初始化。

最好在 onStart 方法中使用 Preferences,而不是 onCreate 或 onResume 方法。

【讨论】:

    【解决方案2】:

    SharedPreferences 不同于首选项。您应该像下面的示例代码一样调用 getSharedPreferences:

    public static String APP_PREFERENCES = "appPreferences";
    public static String PREFERENCE_SAMPLE = "prefSample";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //get Shared preferences
        sharedPreferences = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
    
        //retrieve a preference
        String str = sharedPreferences.getString(PREFERENCE_SAMPLE, "");
    
    }
    

    【讨论】:

      【解决方案3】:

      将此作为共享偏好类

      public class PreferenceConnector{
      public static final String PREF_NAME = "PEOPLE_PREFERENCES";
      public static final int MODE = Context.MODE_PRIVATE;
      
      /*public static final String NAME = "NAME";
      public static final String SURNAME = "SURNAME";
      public static final String AGE = "AGE";*/
      
      public static void writeBoolean(Context context, String key, boolean value) {
          getEditor(context).putBoolean(key, value).commit();
      }
      
      public static boolean readBoolean(Context context, String key, boolean defValue) {
          return getPreferences(context).getBoolean(key, defValue);
      }
      
      public static void writeInteger(Context context, String key, int value) {
          getEditor(context).putInt(key, value).commit();
      
      }
      
      public static int readInteger(Context context, String key, int defValue) {
          return getPreferences(context).getInt(key, defValue);
      }
      
      public static void writeString(Context context, String key, String value) {
          getEditor(context).putString(key, value).commit();
      
      }
      
      public static String readString(Context context, String key, String defValue) {
          return getPreferences(context).getString(key, defValue);
      }
      
      public static void writeFloat(Context context, String key, float value) {
          getEditor(context).putFloat(key, value).commit();
      }
      
      public static float readFloat(Context context, String key, float defValue) {
          return getPreferences(context).getFloat(key, defValue);
      }
      
      public static void writeLong(Context context, String key, long value) {
          getEditor(context).putLong(key, value).commit();
      }
      
      public static long readLong(Context context, String key, long defValue) {
          return getPreferences(context).getLong(key, defValue);
      }
      
      public static SharedPreferences getPreferences(Context context) {
          return context.getSharedPreferences(PREF_NAME, MODE);
      }
      
      public static Editor getEditor(Context context) {
          return getPreferences(context).edit();
      }
      

      }

      【讨论】:

      • 您可以使用 'PreferenceConnector.writeString(settings.this,"pwd", temp_cp);' 写入并使用 'pwd=PreferenceConnector.readString(settings.this, "pwd","") 读取;'
      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 2012-06-01
      • 2016-04-04
      • 2013-09-16
      相关资源
      最近更新 更多