user2507301
user2507301

Reputation: 153

Cannot save value in SharedPreferences

I am encountering a problem in my Android application where my app crashes when I try to save a value. I am making an online currency converter. Here is my part of my code:

View.OnClickListener myhandler1 = new View.OnClickListener() {
    @Override
    public void onClick(View v) {



        String text1 = spinner1.getSelectedItem().toString().trim();
        String text2 = spinner2.getSelectedItem().toString().trim();

        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            try {
            convertvalues("USD", "EUR");
            float k = Float.parseFloat(convertvalues("USD", "EUR"));
            SharedPreferences settings = getActivity().getSharedPreferences("Pref", 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putFloat("dollarseuros", k);
            editor.commit();
            }
            catch (Exception e) {
                SharedPreferences settings = getActivity().getSharedPreferences("Pref", 0);
                float val = Float.parseFloat(edittextdollars.getText().toString());
                float k = Float.parseFloat(convertvalues("USD", "EUR"));
                DecimalFormat df = new DecimalFormat(".##");
                String l = df.format(k * val);
                edittexteuros.setText(settings.getString("dollarseuros", l));
            }
        }

What my app is supposed to do is to get the value of the USD to EUR rate, then save it until there is no internet connection. It is supposed to use the cached value when no internet connection is available.

Here is my LogCat:

07-24 00:16:14.804: E/AndroidRuntime(1298): FATAL EXCEPTION: main
07-24 00:16:14.804: E/AndroidRuntime(1298): java.lang.NumberFormatException: Invalid float: ""
07-24 00:16:14.804: E/AndroidRuntime(1298):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at java.lang.StringToReal.parseFloat(StringToReal.java:289)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at java.lang.Float.parseFloat(Float.java:300)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at com.example.currencyconverter.MainActivity$1.onClick(MainActivity.java:240)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.view.View.performClick(View.java:4204)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.view.View$PerformClick.run(View.java:17355)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.os.Handler.handleCallback(Handler.java:725)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.os.Looper.loop(Looper.java:137)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at java.lang.reflect.Method.invokeNative(Native Method)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at java.lang.reflect.Method.invoke(Method.java:511)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-24 00:16:14.804: E/AndroidRuntime(1298):     at dalvik.system.NativeStart.main(Native Method)

What is wrong? I have checked Google and StackOverflow, but nothing solves my problem. Any help would be greatly appreciated.

Upvotes: 1

Views: 219

Answers (1)

Vikram
Vikram

Reputation: 51581

You are getting a NumberFormatException which means, in Float.parseFloat(stringToParse), stringToParse cannot be parsed to a float.

For example, Float.parseFloat("") or Float.parseFloat("Not a float") will throw a NumberFormatException because the empty string "" or "Now a float" cannot be parsed to a float value. Check what you are feeding to Float.parseFloat() and you will overcome this issue.

Edit

Based on convertvalues() method you have posted now:

public String convertvalues(String convertfrom, String convertto) {
                double current;
                double val = Double.parseDouble(
                                edittextdollars.getText().toString());
                DecimalFormat df = new DecimalFormat(".##");
                 YahooCurrencyConverter ycc = new YahooCurrencyConverter();                  
                     try {
                        current = ycc.convert(convertfrom, convertto);
                        edittexteuros.setText(df.format(val*current)); 
                        return "";    //           <----------- Problem
                        }
                    catch (Exception e) {

                        return "";    //           <----------- Could be a problem
                    } 
}

Your convertvalues() method returns an empty string no matter what. You are returning an empty string in both try and catch block. Is this what you really want to return?

Upvotes: 2

Related Questions