正在研究一个基本的小费计算器并遇到以下程序崩溃。崩溃的日志是:
06-24 01:03:43.602: E/AndroidRuntime(27213): FATAL EXCEPTION: main
06-24 01:03:43.602: E/AndroidRuntime(27213): java.lang.NumberFormatException: Invalid double: "android.widget.EditText@41634f08"
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.StringToReal.parseDouble(StringToReal.java:263)
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.Double.parseDouble(Double.java:295)
06-24 01:03:43.602: E/AndroidRuntime(27213): at mkelleyjr.example.tippycalc.MainActivity$1.onClick(MainActivity.java:30)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.view.View.performClick(View.java:3540)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.view.View$PerformClick.run(View.java:14167)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.os.Handler.handleCallback(Handler.java:605)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.os.Looper.loop(Looper.java:137)
06-24 01:03:43.602: E/AndroidRuntime(27213): at android.app.ActivityThread.main(ActivityThread.java:4558)
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 01:03:43.602: E/AndroidRuntime(27213): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 01:03:43.602: E/AndroidRuntime(27213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-24 01:03:43.602: E/AndroidRuntime(27213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-24 01:03:43.602: E/AndroidRuntime(27213): at dalvik.system.NativeStart.main(Native Method)
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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="47dp"
android:background="@android:color/holo_blue_light"
android:text="Total Bill Amount"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/holo_orange_dark" />
<TextView
android:id="@+id/res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/button1"
android:layout_marginBottom="14dp"
android:text="Result : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/res"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginBottom="34dp"
android:background="@android:color/black"
android:text="Calculate"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="96dp"
android:background="@android:color/holo_green_dark"
android:text="Enter Tip % :"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/holo_red_dark" />
<EditText
android:id="@+id/bill_amt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@id/textView1"
android:layout_marginTop="41dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/bill_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="22dp"
android:ems="10" />
</RelativeLayout>
MainActivity.java是:
package mkelleyjr.example.tippycalc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
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);
final EditText amt = (EditText) findViewById(R.id.bill_amt);
final EditText tip = (EditText) findViewById(R.id.bill_percent);
final TextView result = (TextView) findViewById(R.id.res);
Button calc = (Button) findViewById(R.id.button1);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());
double tip_cal = (amount * tip_per) / 100;
result.setText("Result : " + Double.toString(tip_cal));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
目前还不确定为什么它会崩溃说无效双。任何帮助是极大的赞赏!提前致谢!
答案 0 :(得分:5)
java.lang.NumberFormatException:
Invalid double:"android.widget.EditText@41634f08"
您将editText
Object
的引用作为String
的{{1}}方法parseDouble(String)
和Double.parseDouble(amt.toString())
传递,而不是
Double.parseDouble(tip.toString());
或
Double.parseDouble(editText.getText().toString())
代码。
答案 1 :(得分:1)
double amount = Double.parseDouble(amt.toString());
double tip_per = Double.parseDouble(tip.toString());
错了
double amount = Double.parseDouble(amt.getText().toString());
double tip_per = Double.parseDouble(tip.getText().toString());
正确的方法
并添加
在EditText xml中
android:inputType="numberDecimal|numberSigned"
答案 2 :(得分:0)
您以错误的方式从EditText
获取文字。通过调用amt.toString()
,您只需获得该类的默认Java
字符串表示形式。正确的方法是:
EditText.getText().toString()
答案 3 :(得分:0)
double amount = Double.parseDouble(amt.toString());
此处amt.toString()
将返回amt EditText
的字符串表示形式。我们需要在amt TextView中输入文本,因此请使用以下命令:
double amount = Double.parseDouble(amt.getText().toString());