我陷入了麻烦。我有一个PreferenceManager,它跟踪一些具有默认值的首选项,并且可以在运行时由用户设置。前几天我正在测试它,并输入一个显然不是我设置它的方式处理的值。我想加载xml文件中设置的默认值,以便我的应用程序可以再次运行。现在,它到达加载以下值的部分

dR.sethRVMax(Integer.parseInt(prefs.getString("hRVMaxKey", "100")));


然后它吐出这个错误

02-23 20:35:31.454: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main
02-23 20:35:31.454: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{cpe495.smartapp/cpe495.smartapp.SmartApp}: java.lang.NumberFormatException: unable to parse '7p-' as integer
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.os.Looper.loop(Looper.java:123)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invoke(Method.java:521)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at dalvik.system.NativeStart.main(Native Method)
02-23 20:35:31.454: ERROR/AndroidRuntime(276): Caused by: java.lang.NumberFormatException: unable to parse '7p-' as integer
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at java.lang.Integer.parse(Integer.java:433)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at java.lang.Integer.parseInt(Integer.java:422)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at java.lang.Integer.parseInt(Integer.java:382)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at cpe495.smartapp.SmartApp.onCreate(SmartApp.java:90)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-23 20:35:31.454: ERROR/AndroidRuntime(276):     ... 11 more


我想从xml加载完整的默认值,而不是尝试加载最后保存的首选项。我尝试了以下方法,但它不起作用:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
PreferenceManager.setDefaultValues(this, R.xml.settings, true);
prefs.registerOnSharedPreferenceChangeListener(this);


如果需要更多信息,请告诉我。

提前致谢!

最佳答案

要将首选项重置为默认值,您需要先清除它们:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().clear().commit();
PreferenceManager.setDefaultValues(this, R.xml.settings, true);


有关更多信息,请查看docs for PreferenceManager。他们在描述readAgainsetDefaultVaules参数时也讨论了这种情况:


  注意:这不会将首选项恢复为默认值。对于该功能,请使用getDefaultSharedPreferences(Context)并清除它,然后调用此方法并将此参数设置为true

09-26 02:54