当我尝试编译我的应用时,我有一个数字格式异常。以下是错误的来源:
EditText a = (EditText) findViewById(R.id.etPred_density);
setPredDensity(Double.parseDouble(a.getText().toString()));
问题在于第二行。
以下是activity_main.xml中引用的.xml文件:
<EditText
android:id="@+id/etPred_density"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center_vertical|center_horizontal"
android:hint="0.2"
android:inputType="numberDecimal" />
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bham.eco"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ecologo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="bham.eco.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="bham.eco.Sequence"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Sequence" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="bham.eco.Rules"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Rules" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
错误日志:
FATAL EXCEPTION: main
Process: bham.eco, PID: 2543
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
... 11 more
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:248)
at java.lang.Double.parseDouble(Double.java:295)
at bham.eco.MainActivity.assignParams(MainActivity.java:138)
at bham.eco.MainActivity.playit(MainActivity.java:123)
at bham.eco.MainActivity.runit(MainActivity.java:63)
... 14 more
我在这里做错了什么?该异常似乎是由parseDouble()方法引起的。
答案 0 :(得分:2)
EditText a = (EditText) findViewById(R.id.etPred_density);
String densityValue = a.getText().toString();
Double density = null;
try
{
density = Double.parseDouble(densityValue);
}
catch(NumberFormatException e)
{
//Not a number, need to handle this
}
// Free to carry on with density
setPredDensity(density);
答案 1 :(得分:1)
使用Regex
检查Double
格式:
EditText a = (EditText) findViewById(R.id.etPred_density);
String densityValue = a.getText().toString();
Pattern.compile("\\d+\\.\\d+");
Matcher matcher = pattern.matcher(densityValue);
boolean isDouble = matcher.matches();
if(isDouble) {
setPredDensity(Double.parseDouble(densityValue));
}
答案 2 :(得分:1)
首先,我们必须在try catch块中执行此类操作。 然后添加任何逻辑..
像
这样的东西 try{
// logic
}catch(Exception e){
e.printStackStrace();
}
因此,如果像Josef所说的解析中出现任何错误数据,它可以处理..
答案 3 :(得分:0)
请检查
EditText a = (EditText) findViewById(R.id.etPred_density);
if (!a.getText().toString().isEmpty()){
setPredDensity(Double.parseDouble(a.getText().toString()));
}
答案 4 :(得分:0)
尝试使用a.getEditableText()
代替setPredDensity(Double.parseDouble(a.getText().toString())
);
答案 5 :(得分:0)
尝试检查是否:
真的是双重
EditText a = (EditText) findViewById(R.id.etPred_density);
if (a.getText().toString().isEmpty() || a.getText() == null)
{
doubleValueInvalid();
}
try
{
setPredDensity(Double.parseDouble(a.getText().toString()));
}
catch(NumberFormatException error){ //if its not double
doubleValueInvalid();
}
在Oracle文档中查看parseDouble抛出的内容: http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)