我正在尝试使用一些可编辑的文本以Double
格式获得一些价值。
将edittext值转换为double时显示number precision error
。
my activity.xml:
<EditText
android:id="@+id/edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="57dp"
android:layout_marginRight="31dp"
android:ems="10"
android:inputType="number" >
</EditText>
和我的主要活动代码我试图
从edittext中检索值为double但显示
NumberFormatException无效的双倍:
package com.example.habijabi;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText et,et1;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.edit1);
String s=et.getText().toString();
double data;
data=Double.parseDouble(s);
System.out.println(s);
}
}
在logcat中显示:
11-25 14:07:13.386: I/Process(1105): Sending signal. PID: 1105 SIG: 9
11-25 14:07:14.797: W/Trace(1170): Unexpected value from nativeGetEnabledTags: 0
11-25 14:07:14.797: W/Trace(1170): Unexpected value from nativeGetEnabledTags: 0
11-25 14:07:14.906: W/Trace(1170): Unexpected value from nativeGetEnabledTags: 0
11-25 14:07:14.906: W/Trace(1170): Unexpected value from nativeGetEnabledTags: 0
11-25 14:07:15.256: D/AndroidRuntime(1170): Shutting down VM
11-25 14:07:15.256: W/dalvikvm(1170): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
11-25 14:07:15.286: E/AndroidRuntime(1170): FATAL EXCEPTION: main
11-25 14:07:15.286: E/AndroidRuntime(1170): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.habijabi/com.example.habijabi.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.os.Looper.loop(Looper.java:137)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread.main(ActivityThread.java:5039)
11-25 14:07:15.286: E/AndroidRuntime(1170): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:07:15.286: E/AndroidRuntime(1170): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:07:15.286: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-25 14:07:15.286: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-25 14:07:15.286: E/AndroidRuntime(1170): at dalvik.system.NativeStart.main(Native Method)
11-25 14:07:15.286: E/AndroidRuntime(1170): Caused by: java.lang.NumberFormatException: Invalid double: ""
11-25 14:07:15.286: E/AndroidRuntime(1170): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-25 14:07:15.286: E/AndroidRuntime(1170): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
11-25 14:07:15.286: E/AndroidRuntime(1170): at java.lang.Double.parseDouble(Double.java:295)
11-25 14:07:15.286: E/AndroidRuntime(1170): at com.example.habijabi.MainActivity.onCreate(MainActivity.java:27)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.Activity.performCreate(Activity.java:5104)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-25 14:07:15.286: E/AndroidRuntime(1170): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-25 14:07:15.286: E/AndroidRuntime(1170): ... 11 more
答案 0 :(得分:2)
原因是,""
不是有效的双倍。您需要先测试String或捕获此类异常
double data;
try {
data = new Double(et.getText().toString());
} catch (NumberFormatException e) {
data = 0; // your default value
}
重点:在投射任何内容时添加
try and catch
。