【问题标题】:Android Studio pref Editor.putString identifier expected [duplicate]预期 Android Studio 首选项 Editor.putString 标识符 [重复]
【发布时间】:2016-09-20 15:43:16
【问题描述】:

在下面的代码中获取 - 无法解析符号“putString”“putInt”“commit”。请帮助。

      public class Alpha extends Activity {

      public static final String GAME_PREFERENCES = "GamePrefs";
      SharedPreferences settings =getSharedPreferences(GAME_PREFERENCES,MODE_PRIVATE);

      SharedPreferences.Editor prefEditor = settings.edit();
      prefEditor.putString("Gama x","Blue Spec");
      prefEditor.putInt("Wave",292);
      prefEditor.commit();


 }

【问题讨论】:

  • 将其移入 onCreate。 (getSharedPreferences 也是)

标签: java android android-studio sharedpreferences


【解决方案1】:

在开始随机编码之前,先看看一些 android 教程,例如 this is the best在我看来

之后就可以开始编码了。

顺便说一句,解决方案正在“onCreate”方法中移动它:

public class Alpha extends Activity {
  public static final String GAME_PREFERENCES = "GamePrefs";
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      SharedPreferences settings =getSharedPreferences(GAME_PREFERENCES,MODE_PRIVATE);

      SharedPreferences.Editor prefEditor = settings.edit();
      prefEditor.putString("Gama x","Blue Spec");
      prefEditor.putInt("Wave",292);
      prefEditor.commit();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    相关资源
    最近更新 更多