当我尝试运行以下代码时,我不断收到错误日志 有错误消息:
03-11 16:00:08.350: E/AndroidRuntime(842): FATAL EXCEPTION: main
03-11 16:00:08.350: E/AndroidRuntime(842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.os.Looper.loop(Looper.java:137)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-11 16:00:08.350: E/AndroidRuntime(842): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:00:08.350: E/AndroidRuntime(842): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:00:08.350: E/AndroidRuntime(842): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 16:00:08.350: E/AndroidRuntime(842): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 16:00:08.350: E/AndroidRuntime(842): at dalvik.system.NativeStart.main(Native Method)
03-11 16:00:08.350: E/AndroidRuntime(842): Caused by: java.lang.NumberFormatException: Invalid double: ""
03-11 16:00:08.350: E/AndroidRuntime(842): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-11 16:00:08.350: E/AndroidRuntime(842): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
03-11 16:00:08.350: E/AndroidRuntime(842): at java.lang.Double.parseDouble(Double.java:295)
03-11 16:00:08.350: E/AndroidRuntime(842): at com.example.calculator.MainActivity.onCreate(MainActivity.java:21)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.Activity.performCreate(Activity.java:5104)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 16:00:08.350: E/AndroidRuntime(842): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-11 16:00:08.350: E/AndroidRuntime(842): ... 11 more
03-11 16:05:08.990: E/AndroidRuntime(959): FATAL EXCEPTION: main
03-11 16:05:08.990: E/AndroidRuntime(959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.os.Looper.loop(Looper.java:137)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-11 16:05:08.990: E/AndroidRuntime(959): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:05:08.990: E/AndroidRuntime(959): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:05:08.990: E/AndroidRuntime(959): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 16:05:08.990: E/AndroidRuntime(959): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 16:05:08.990: E/AndroidRuntime(959): at dalvik.system.NativeStart.main(Native Method)
03-11 16:05:08.990: E/AndroidRuntime(959): Caused by: java.lang.NumberFormatException: Invalid double: ""
03-11 16:05:08.990: E/AndroidRuntime(959): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-11 16:05:08.990: E/AndroidRuntime(959): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
03-11 16:05:08.990: E/AndroidRuntime(959): at java.lang.Double.parseDouble(Double.java:295)
03-11 16:05:08.990: E/AndroidRuntime(959): at com.example.calculator.MainActivity.onCreate(MainActivity.java:21)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.Activity.performCreate(Activity.java:5104)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 16:05:08.990: E/AndroidRuntime(959): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-11 16:05:08.990: E/AndroidRuntime(959): ... 11 more
MainActivity.java
package com.example.calculator;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tPrincipalAmount = (TextView)findViewById(R.id.tPrincipalAmount);
EditText editPrincipalAmount= (EditText)findViewById(R.id.editPrincipalAmount);
String String_Principal_amount = editPrincipalAmount.getText().toString();
double DbPrincipalAmount = Double.parseDouble(String_Principal_amount);
TextView tInterestRate =(TextView) findViewById(R.id.tInterestRate);
EditText editInterestRate =(EditText) findViewById(R.id.editInterestRate);
String string_editInterestRate =editInterestRate.getText().toString();
double DbInterestRate = Double.parseDouble(string_editInterestRate);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tPrincipalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:text="Principal Amount ($)" />
<EditText
android:id="@+id/editPrincipalAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tPrincipalAmount"
android:layout_below="@+id/tPrincipalAmount"
android:ems="10" />
<TextView
android:id="@+id/tInterestRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editPrincipalAmount"
android:layout_below="@+id/editPrincipalAmount"
android:layout_marginTop="26dp"
android:text="Interest Rate" />
<EditText
android:id="@+id/editInterestRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tInterestRate"
android:layout_marginTop="27dp"
android:ems="10" />
</RelativeLayout>
谢谢
答案 0 :(得分:0)
您要解析为string
的{{1}}不包含值double
的原因。您应该检查NumberFormatException
是否包含有效值。现在,检查如下......
string