java - Cannot save value in SharedPreferences -
i encountering problem in android application app crashes when try save value. making online currency converter. here part of 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 app supposed value of usd eur rate, save until there no internet connection. supposed use cached value when no internet connection available.
here 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): @ java.lang.stringtoreal.invalidreal(stringtoreal.java:63) 07-24 00:16:14.804: e/androidruntime(1298): @ java.lang.stringtoreal.parsefloat(stringtoreal.java:289) 07-24 00:16:14.804: e/androidruntime(1298): @ java.lang.float.parsefloat(float.java:300) 07-24 00:16:14.804: e/androidruntime(1298): @ com.example.currencyconverter.mainactivity$1.onclick(mainactivity.java:240) 07-24 00:16:14.804: e/androidruntime(1298): @ android.view.view.performclick(view.java:4204) 07-24 00:16:14.804: e/androidruntime(1298): @ android.view.view$performclick.run(view.java:17355) 07-24 00:16:14.804: e/androidruntime(1298): @ android.os.handler.handlecallback(handler.java:725) 07-24 00:16:14.804: e/androidruntime(1298): @ android.os.handler.dispatchmessage(handler.java:92) 07-24 00:16:14.804: e/androidruntime(1298): @ android.os.looper.loop(looper.java:137) 07-24 00:16:14.804: e/androidruntime(1298): @ android.app.activitythread.main(activitythread.java:5041) 07-24 00:16:14.804: e/androidruntime(1298): @ java.lang.reflect.method.invokenative(native method) 07-24 00:16:14.804: e/androidruntime(1298): @ java.lang.reflect.method.invoke(method.java:511) 07-24 00:16:14.804: e/androidruntime(1298): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-24 00:16:14.804: e/androidruntime(1298): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-24 00:16:14.804: e/androidruntime(1298): @ dalvik.system.nativestart.main(native method)
what wrong? have checked google , stackoverflow, nothing solves problem. appreciated.
you getting numberformatexception
means, in float.parsefloat(stringtoparse), stringtoparse
cannot parsed float.
for example, float.parsefloat("")
or float.parsefloat("not float")
throw numberformatexception
because empty string ""
or "now float"
cannot parsed float value. check feeding float.parsefloat() , overcome issue.
edit
based on convertvalues()
method 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 ""; // <----------- problem } }
your convertvalues()
method returns empty string no matter what. returning empty string in both try , catch block. want return?
Comments
Post a Comment