I want to implement the preservation of the Spinner value and the subsequent substitution of it in the same Spinner . That is, when entering the application settings, a theme will be installed that is stored in the spinner (based on the value in SharedPreferences ). And then you can choose a value from the Spinner , then it is again saved in SharedPreferences .
package com.churkin.oneapp; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; public class Settings extends AppCompatActivity { Spinner spinnercolor; SharedPreferences colorPref; final String COLOR_PREF = "COLOR_PREF"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); setContentView(R.layout.settings); setTitle("Настройки"); spinnercolor = (Spinner) findViewById(R.id.spinnercolor); ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.spinnersettings, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnercolor.setAdapter(adapter); spinLoad(); spinnercolor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String[] choose = getResources().getStringArray(R.array.spinnersettings); String item = (String)adapterView.getItemAtPosition(i); int selectedPosition = spinnercolor.getSelectedItemPosition(); spinSave(selectedPosition); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } public void spinSave(int numberPosition) { colorPref = getPreferences(MODE_PRIVATE); SharedPreferences.Editor ed = colorPref.edit(); ed.putInt(COLOR_PREF, numberPosition); } public void spinLoad() { colorPref = getPreferences(MODE_PRIVATE); int savedPosition = colorPref.getInt(COLOR_PREF, 0); spinnercolor.setSelection(savedPosition); } } This is the current code, when entering the settings with this code crashes. Why?
Error log:
07-28 09:54:31.833 3456-3456/? E/libprocessgroup: failed to make and chown /acct/uid_10059: Read-only file system 07-28 09:56:38.379 3456-3456/com.churkin.oneapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.churkin.oneapp, PID: 3456 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.churkin.oneapp/com.churkin.oneapp.Settings}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:239) at com.churkin.oneapp.Settings.spinLoad(Settings.java:50) at com.churkin.oneapp.Settings.onCreate(Settings.java:28) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)