如何使用PlainText获取输入?

时间:2019-07-07 15:25:05

标签: java android

firstNumbersecondNumber的类型为EditText。我正在尝试从firstNumbersecondNumber获取值并将它们转换为类型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

5 个答案:

答案 0 :(得分:0)

似乎您的错误是:

Caused by: java.lang.NumberFormatException: For input string: "Enter First Number"

确保不要将函数与数字一起使用。 因此,如果您使用的是编辑文字,请确保只有数字

答案 1 :(得分:0)

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

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

因此只能输入数字。

答案 2 :(得分:0)

您必须将editText限制为仅具有数字值。将此添加到要在其中添加数值的editTexts中

android:inputType="number" 

android:inputType="numberDecimal

您也可以使用

firstNumber.setInputType(InputType.TYPE_CLASS_NUMBER);

答案 3 :(得分:0)

您不能只将不包含可分析的Float的字符串抛出来进行parseFloat并期望它进行解析,这就是为什么出现NumberFormatException的原因。

答案 4 :(得分:0)

尝试使用Float.valueOf()

float num1 = Float.valueOf(firstNumber.getText().toString());
float num2 = Float.valueOf(secondNumber.getText().toString());

并添加

android:inputType="numberDecimal

发送到您的EditText's