使用共享首选项保存微调器位置或值

Saving the spinner position or value using Shared Preference

我在我的片段 class 中,它有包含语言环境的微调器..用于文本到语音

    mylocale = localeList.get(position);
    textToSpeech.setLanguage(mylocale);
    spinnerposition = Integer.valueOf(String.valueOf(localeList.get(position)));

我想使用 sharedpreferences 保存位置,这就是我正在尝试的:

 public void savepos(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putInt(POSITION , spinnerposition);
    editor.apply();
}

我已将微调器位置声明为全局变量

int spinnerposition;

这是我检索位置的方法:

public void saveposload(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    int position = sharedPreferences.getInt(POSITION ,1);

    mspinner.setSelection(sharedPreferences.getInt(POSITION, position));
}

但是我做不到,应用崩溃请帮忙

这是日志中的异常

2018-12-30 15:13:49.904 3611-3611/my.uapp.com E/AndroidRuntime: FATAL EXCEPTION: main
Process: my.uapp.com, PID: 3611
java.lang.NumberFormatException: For input string: "it_IT"
    at java.lang.Integer.parseInt(Integer.java:521)
    at java.lang.Integer.valueOf(Integer.java:611)
    at my.uapp.com.Tab0$override.onItemSelected(Tab0.java:363)
    at my.uapp.com.Tab0$override.access$dispatch(Tab0.java)
    at my.uapp.com.Tab0.onItemSelected(Tab0.java)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:931)
    at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:920)
    at android.widget.AdapterView.-wrap1(AdapterView.java)
    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:890)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

localeList 是您存储所有区域设置的列表,例如 "it_IT" 等吗?
如果是这种情况,那么:

localeList.get(position)

是一个 String 并且不能用这一行解析为 int

spinnerposition = Integer.valueOf(String.valueOf(localeList.get(position)));

也许你只需要这个:

public void saveposload(){
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    mspinner.setSelection(sharedPreferences.getInt(POSITION, 0));
}

确保在设置适配器后调用 saveposload()

试试下面的方法:

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
         mylocale = localeList.get(position);
         textToSpeech.setLanguage(mylocale);
         savpos(position);
    }

    public void savepos(int position) {
        sharedPreferences = this.getActivity().getSharedPreferences("stored_position", Context.MODE_PRIVATE)
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("pos" , position).apply;
     }

   public void saveposload(){
      sharedPreferences = this.getActivity().getSharedPreferences("stored_position", Context.MODE_PRIVATE)
      int position = sharedPreferences.getInt("pos",0);  //zero being the default value
      mspinner.setSelection(position);
    }

在 saveposload() 上放置一个断点并检查是否正确或现在检索位置。你在哪里打电话给 saveposload()

并且您不需要在 private Spinner firstSpinner;

中存储位置