java Android 首选项错误,“字符串无法转换为 int”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17844511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 15:12:42  来源:igfitidea点击:

Android Preferences error, "String cannot be cast to int"

javaandroidstringintegersharedpreferences

提问by Waza_Be

I'm trying to setup a preferences activity but my app keeps crashing and I get the following logcat:

我正在尝试设置一个首选项活动,但我的应用程序不断崩溃,我得到以下 logcat:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appthing.myapp/com.appthing.myapp.Main}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer 07-24 16:37:59.556: E/AndroidRuntime(17384): at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)

致命异常:main java.lang.RuntimeException:无法启动活动 ComponentInfo{com.appthing.myapp/com.appthing.myapp.Main}:java.lang.ClassCastException:java.lang.String 无法转换为 java.lang。整数

引起:java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer 07-24 16:37:59.556: E/AndroidRuntime(17384): at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.爪哇:240)

In my Mainactivity I have the following code inside the onResume()method:

在我的Main活动中,我在onResume()方法中有以下代码:

    SeekBar tipSeekBar = (SeekBar) findViewById(R.id.tipSeekBar);
    SeekBar splitSeekBar = (SeekBar) findViewById(R.id.splitSeekBar);

    SharedPreferences preferences = PreferenceManager
            .getDefaultSharedPreferences(this);

    tipSeekBar.setProgress(preferences.getInt("defaultTip", 15));
    splitSeekBar.setProgress(preferences.getInt("defaultSplit", 1));
    tipSeekBar.setMax(preferences.getInt("maxTip", 25));
    splitSeekBar.setMax(preferences.getInt("maxSplit", 10));

Here is what I have in the Preference class (as requested):

这是我在 Preference 类中的内容(根据要求):

addPreferencesFromResource(R.layout.preferences);
// I was told in tutorials this is all I need in the oncreate method

I don't understand why its saying something about a string. All my values are integers and I am using android:inputType="number"to make sure only an int can be entered. I have also tried uninstalling and re-installing the app to clear the cache and nothing works.

我不明白为什么它说的是字符串。我所有的值都是整数,我android:inputType="number"用来确保只能输入一个 int。我还尝试卸载并重新安装该应用程序以清除缓存,但没有任何效果。

RESOLVED:

解决:

"Your Preferences in XML, even if you set android:inputType="number" are still stored as a String"(by Waza_Be). All I had to do is do Integer.parseInt()to grab the correct value.

“您在 XML 中的首选项,即使您设置 android:inputType="number" 仍然存储为字符串”(由Waza_Be 提供)。我所要做的就是Integer.parseInt()获取正确的值。

回答by Waza_Be

Your Preferences in XML, even if you set android:inputType="number"are still stored as a String

您在 XML 中的首选项,即使您设置android:inputType="number"仍然存储为字符串

You have 2 choices:

您有 2 个选择:

1) the 'not-so-nice': Integer.parseInt( preferences.getString("defaultTip", "15"));

1)“不太好”: Integer.parseInt( preferences.getString("defaultTip", "15"));

2) Using your own type of Integer Preference. More complicated to set in first place but really better (similar question here: https://stackoverflow.com/a/3755608/327402)

2) 使用您自己的整数首选项类型。首先设置更复杂,但确实更好(这里有类似的问题:https: //stackoverflow.com/a/3755608/327402

回答by Levon Petrosyan

The exception can also be thrown if

也可以抛出异常,如果

  • you are using the same keyfor storing two or more different types of values
  • at first you stored the value as Stringand then changed storing and retrieving implementation for intvalue. For this case just clear the cache.
  • 您正在使用相同key的方式存储两种或多种不同类型的values
  • 首先,您将值存储为String,然后更改了存储和检索int值的实现。对于这种情况,只需清除缓存即可。

回答by Phani

If you are storing the preferences with the right data types, then it should not be problem. Please look at the JavaDochere.

如果您使用正确的数据类型存储首选项,那么应该没有问题。请在此处查看JavaDoc