Reputation: 5239
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.
Upvotes: 3
Views: 3696
Reputation: 5208
This question is maybe the same: How does one declare the type of an Android preference?. It might solve your problem.
Upvotes: 2
Reputation: 6073
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.
Upvotes: 5