Java 如何使用纯文本获取输入?

Java 如何使用纯文本获取输入?,java,android,Java,Android,firstNumber和secondNumber属于EditText类型。我试图从firstNumber和secondNumber中获取值,并将它们转换为typefloat,但当我这样做时,我得到一个异常java.lang.NumberFormatException,我无法找到问题的解决方案。好心的,请帮忙 这是密码 firstNumber = (EditText)findViewById(R.id.firstNumber); secondNumber = (EditText)findViewB

firstNumber
secondNumber
属于EditText类型。我试图从
firstNumber
secondNumber
中获取值,并将它们转换为type
float
,但当我这样做时,我得到一个异常
java.lang.NumberFormatException
,我无法找到问题的解决方案。好心的,请帮忙

这是密码

firstNumber = (EditText)findViewById(R.id.firstNumber);
secondNumber = (EditText)findViewById(R.id.secondNumber);
result = (TextView)findViewById(R.id.result);
s1 = firstNumber.getText().toString();
s2 = secondNumber.getText().toString();
num1 = Float.parseFloat(s1);
num2 = Float.parseFloat(s2);
我得到的Logcat是-

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calcmpttos, PID: 17846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calcmpttos/com.example.calcmpttos.MainActivity}: java.lang.NumberFormatException: For input string: "Enter First Number"
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NumberFormatException: For input string: "Enter First Number"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
    at java.lang.Float.parseFloat(Float.java:451)
    at com.example.calcmpttos.MainActivity.onCreate(MainActivity.java:39)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2019-07-07 20:29:53.693 17846-17846/com.example.calcmpttos I/Process: Sending signal. PID: 17846 SIG: 9

似乎您的错误是:

Caused by: java.lang.NumberFormatException: For input string: "Enter First Number"
确保除了数字外,不要将该函数与任何内容一起使用。
因此,如果使用编辑文本,请确保只有数字

,您可以设置数字以绕过错误:

firstNumber.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
secondNumber.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

因此只能键入数字。

您必须将编辑文本限制为仅包含数字值。将其添加到要添加数值的编辑文本中

android:inputType="number" 

您也可以使用

firstNumber.setInputType(InputType.TYPE_CLASS_NUMBER);

您不能将不包含可解析浮点的字符串抛出到parseFloat并期望它解析它,这就是为什么会出现NumberFormatException。

尝试使用
Float.valueOf()

android:inputType="numberDecimal

对您的
编辑文本的

而言,原因是:java.lang.NumberFormatException:“输入第一个数字”
表示
“输入第一个数字”
不是有效的数字字符串,例如
“115”
“0”
“2019”
。。。因此,它不能被解析为数字类型。正如@Fantômas已经告诉你的,你必须限制你的editText只有数字字符串,看看我的答案。这可能对你有帮助。
float num1 = Float.valueOf(firstNumber.getText().toString());
float num2 = Float.valueOf(secondNumber.getText().toString());
android:inputType="numberDecimal