其他分享
首页 > 其他分享> > android – SharedPreferences.getInt(“cumulative”,0)catch 22 – 如何解决?

android – SharedPreferences.getInt(“cumulative”,0)catch 22 – 如何解决?

作者:互联网

与其他值不同,我可以在每次应用启动时在onCreate()中初始化:

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Editor editor = prefs.edit();
    editor.putInt("re-initiative-value", 0);
    editor.commit();
  }

存储在SharedPreferences中的累积值对我来说是个问题(每次程序启动时我都无法重新初始化它).

因此,每当我尝试prefs.getInt(“cumulative-valuee”,0)时,我都会遇到RuntimeException:

05-12 16:45:50.489: ERROR/AndroidRuntime(1767): FATAL EXCEPTION: main
05-12 16:45:50.489: ERROR/AndroidRuntime(1767): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent { (has extras) }} to activity {com.example.app/com.example.app.MyActivity}: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.access$2800(ActivityThread.java:125)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.os.Looper.loop(Looper.java:123)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at java.lang.reflect.Method.invoke(Method.java:521)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at dalvik.system.NativeStart.main(Native Method)
05-12 16:45:50.489: ERROR/AndroidRuntime(1767): Caused by: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2707)

避免此类异常的好策略是什么?

我想把getInt()调用放在try-catch子句中,这样我就可以在第一次程序启动时编写它,但后来我意识到:不是getInt()中的默认值(第二个参数)这意味着什么?

更新:尝试验证@ MyBD的建议,我添加了以下代码以在首次运行时转储所有首选项名称和值:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);

Map<String,?> allPrefs = prefs.getAll();
for (Map.Entry<String, ?> entry : allPrefs.entrySet())
  Log.i("allPrefs", entry.getKey() + "/" + entry.getValue());

生成的日志显示没有类型不匹配.

这必须是伪装成java.lang.ClassCastException的另一个问题.

解决方法:

从您的例外情况来看,您似乎确实为字符串首选项设置了此名称,而不是此首选项不存在:

05-12 16:45:50.489: ERROR/AndroidRuntime(1767): Caused by: java.lang.ClassCastException: java.lang.String
05-12 16:45:50.489: ERROR/AndroidRuntime(1767):     at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2707)

documentation开始:

Returns the preference value if it
exists, or defValue. Throws
ClassCastException if there is a
preference with this name that is not
an int.

如果你更改它的名字,它应该工作.

标签:android,android-preferences
来源: https://codeday.me/bug/20190827/1735815.html