在我的应用程序中我有三个editText,用户可以在其中放置一个数字。然后当用户按下按钮时,我将计算结果转换为另一个editext。
这是我的代码,但是应用程序强制下来......请帮忙吗?
代码:
float result = (float) ((((new Double(input11.getText().toString())
+ new Double(input21.getText().toString()))/2)*0.3)+ (new Double(input31.getText().toString())*0.7));
vprosvasis.setText(Float.toString(result));}
计算:
float genikosvathmos = (float) ((new Double(vprosvasis.getText().toString())+new Double(vprosvasis7.getText().toString())+ new Double(vprosvasis2.getText().toString())+new Double(vprosvasis3.getText().toString())
+new Double(vprosvasis4.getText().toString())+new Double(vprosvasis5.getText().toString())+new Double(vprosvasis6.getText().toString()))/7);
float moria=(float) (((new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100)));
Toast.makeText(thetiki.this, "Genikos vathmos"+moria , Toast.LENGTH_SHORT).show();
logcat的:
02-23 13:57:50.255: WARN/dalvikvm(20923): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): FATAL EXCEPTION: main
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): java.lang.NumberFormatException:
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl(Native Method)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at java.lang.Float.parseFloat(Float.java:291)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at java.lang.Float.<init>(Float.java:111)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at kostas.menu.moria.thetiki$8.onClick(thetiki.java:372)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.view.View.performClick(View.java:2461)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.view.View$PerformClick.run(View.java:8888)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.os.Handler.handleCallback(Handler.java:587)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.os.Looper.loop(Looper.java:123)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at java.lang.reflect.Method.invoke(Method.java:521)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-23 13:57:50.364: ERROR/AndroidRuntime(20923): at dalvik.system.NativeStart.main(Native Method)
02-23 13:57:50.399: WARN/ActivityManager(2944): Force finishing activity kostas.menu.moria/.thetiki
答案 0 :(得分:3)
你得到NumberFormatException
。这是因为你的float包含字符串,无法解析,在这里:new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100))
简单地摆脱“”
编辑:
此外,作为vprosvasis
等,是EditTexts或其他类型的输入,您必须以适当的方式转换它们的值,以便能够使用它们进行计算。
一种可能性是:
float vprosvasisFloat = Float.parseFloat(vprosvasis.getText().toString());
然后,您可以使用vprosvasisFloat
代替vprosvasis
答案 1 :(得分:2)
这一行真的很奇怪,你在不应该使用引号的地方,你使用“,”作为小数分隔符。还有一些无用的括号:
float moria = (float) (((new Float ((genikosvathmos*8)+("vprosvasis * 1.3")+("vprosvasis2 * 0,7"))*100)));
你应该写
float moria = (float) ((genikosvathmos * 8.0 + vprosvasis * 1.3 + vprosvasis2 * 0.7) * 100.0);
答案 2 :(得分:0)
将moria转换为字符串,然后执行Tost ..
String.valueOf(moria)