I want to get a float from the preferences. This works fine when the user doesn't enter anything, but when entering 8.23 to the box and saving it, the app crashes on the next start.
MainActivity.java:
float hourly_rate;
SharedPreferences userdata;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
userdata = PreferenceManager.getDefaultSharedPreferences(this);
hourly_rate = userdata.getFloat("hourly_rate", 0.0f);
}
Preferences.xml:
<PreferenceCategory
android:title="@string/general_preferences_title">
<EditTextPreference
android:inputType="numberDecimal"
android:key="hourly_rate"
android:title="@string/hourly_rate_title"
android:summary="@string/hourly_rate_summary" />
</PreferenceCategory>
When I remove the getFloat line, the app doesn't force close.
EditTextPreference stores its content as a String. Float.parseFloat(String) might be good use for you - among with preferences.getString(..) - unless you write an own FloatPreference which stores text input as a float.
This question is maybe the same: How does one declare the type of an Android preference?. It might solve your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With