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

时间:2018-12-30 11:35:06

标签: android android-fragments android-view android-spinner

我在我的Fragment类中,该类的微调器包含区域设置。用于文本转语音

    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)

2 个答案:

答案 0 :(得分:0)

localeList是存储所有语言环境(例如"it_IT"等)的列表吗?
如果是这种情况,那么:

localeList.get(position)

字符串,并且不能用以下行将其解析为 int

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

也许您只需要这个:

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

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

答案 1 :(得分:0)

尝试以下方法:

    @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;