将SharedPreferences
与偏好结合起来
我希望在设置中使用整数,应用程序崩溃:
MainActivity.java:
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
int car = SP.getInt("hourly_key", 0);
int wage = SP.getInt("car_key",0);
prefs_general.xml:
<EditTextPreference
android:capitalize="words"
android:defaultValue="@integer/pref_default_hourly_name"
android:inputType="number"
android:key="hourly_key"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_hourly_name"
/>
<EditTextPreference
android:capitalize="words"
android:defaultValue="@integer/pref_default_car_name"
android:inputType="number"
android:key="car_key"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_car_name" />
的strings.xml:
<string name="pref_title_hourly_name">Hourly wage</string>
<integer name="pref_default_hourly_name">30</integer>
<string name="pref_title_car_name">Daily car expense</string>
<integer name="pref_default_car_name">15</integer>
日志:
4-08 09:30:03.805 14350-14350/com.michlind.nanytime E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.michlind.nanytime, PID: 14350
java.lang.RuntimeException: Unable to resume activity {com.michlind.nanytime/com.michlind.nanytime.MainActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:242)
at com.michlind.nanytime.MainActivity.onPostResume(MainActivity.java:32)
at android.app.Activity.performResume(Activity.java:6351)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:2)
当您使用preference
加载String
时,getInt()
类型为getString()
,这是错误的。你应该使用Integer.parseInt(pref.getString("v","9"));
代替。
修改强>
让你实际上将它保存为整数,你必须编写自己的自定义类或否则你应该使用
libC