gpt4 book ai didi

java - 如何将EditText输入转换为Float

转载 作者:行者123 更新时间:2023-12-01 11:41:29 24 4
gpt4 key购买 nike

我想将 EditText 输入转换为 float ,但应用程序已停止,我不知道我做错了什么..

这是 java 文件中的代码:

 EditText janeiro1 = (EditText) findViewById(R.id.editText1);
float janeiro = Float.valueOf(janeiro1.getText().toString());

这是 .xml 文件中的代码:

 <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/editText2"
android:layout_above="@+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

这是 logcat 错误输出:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.kiowa.myapp/com.example.kiowa.myapp.c_detalhado}:
java.lang.NumberFormatException: Invalid float: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at com.example.kiowa.myapp.c_detalhado.onCreate(c_detalhado.java:39)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

最佳答案

    usernameInput = (EditText) findViewById(R.id.username); // inOncreateview

// Somewhere in method
float a;

try{
if(!usernameInput.getText().toString().equalsignorecase(""))
a = Float.parseFloat(usernameInput.getText().toString());
}catch(NumberFormatException e){
log.d("Error","Not Convertible");
e.printstacktrace();
}

关于java - 如何将EditText输入转换为Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29493935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com