1

I want to store a float variable as SharedPreferences in my fragment. So I try to set it up and edit it later, but I get an error in the line verbrauch = prefsver.getFloat("key", 0); that the Integer cannot be cast to a Float variable, although I thought I‘m not even using an int there. Am I storing it a wrong way?

My code, it’s just a snipped so I hope I didn’t cut anything relevant:

public class ItemThreeFragment extends Fragment {

    TextView textView11;
    float verbrauch;

    public static ItemThreeFragment newInstance() {
        ItemThreeFragment fragment = new ItemThreeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View view = inflater.inflate(R.layout.fragment_item_three, container, false);

        SharedPreferences prefsver = getActivity().getBaseContext().getSharedPreferences("myPrefsKeyv",
                Context.MODE_PRIVATE);
        verbrauch = prefsver.getFloat("key",0);

        Button button = (Button) view.findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

        final EditText et = (EditText) getActivity().findViewById(R.id.editText);
                String str = et.getText().toString();

        verbrauch = Float.valueOf(str);

                SharedPreferences prefsver = getActivity().getSharedPreferences("myPrefsKeyv",
                        Context.MODE_PRIVATE);
                prefsver.edit().putFloat("key", verbrauch)
                        .apply();
            }
        });
}
1
  • 1
    Try to remove even more stuff, thus isolate the code which produces the exception. And please do not use German, not even as names of variables... Try to create the minimum code which produces the same error. You may then find yourself where your problem is. Commented Jun 11, 2018 at 14:48

2 Answers 2

2

0 is an int literal - not a float. Use 0f (with a suffix), or cast explicitly as (float)0.

Edit:

Also check whether you're actually storing a float, e.g. there could be some leftover value. The method will try to cast what is stored in prefs as float, and in case the cast doesn't succeed, it throws ClassCastException.

Sign up to request clarification or add additional context in comments.

4 Comments

Without the f it is recognized as a double, but even like 0.0fI still get the error
Right, my bad. Though ClassCastException is a bit unusual if you're not using preference screen. Are you sure what's stored inside is actually a float (e.g. check the value using the debugger)?
I think I got it, you gave me the right hint! The last saved value wasn’t a float indeed. Thank you!
Glad it helped - let me edit my answer to put that advice in.
1

verbrauch = prefsver.getFloat("key",0F); would be correct. Note the F after the 0 that makes it a float.

1 Comment

I tried it, I thought of that too but my compiler still returns a ClassCastException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.