如果我按" ="," +"," - "," *"或&#34 ; /"在任何数字之前,我的应用程序崩溃了!我无法找到任何好的答案,我不知道自己需要做什么。
我尝试了多种不同的方法来制作这个计算器,但我仍然遇到同样的问题。我以为某种方式认为If语句与OnCalculation有关。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Button button = (Button) v;
String str = button.getText().toString();
total += str;
EditText edit = (EditText) findViewById(R.id.editText);
edit.setText(total);
}
public void OnAdd(View v) {
temp1 = Double.parseDouble(total);
total = "";
Button button = (Button) v;
String str = button.getText().toString();
sign += str;
EditText edit = (EditText) findViewById(R.id.editText);
edit.setText("");
}
public void OnCalculate(View v) {
EditText edit = (EditText) findViewById(R.id.editText);
String str2 = edit.getText().toString();
temp2 = Double.parseDouble(str2);
Double grand_total = 0.0;
if (sign.equals("+")) {
grand_total = temp1 + temp2;
} else if (sign.equals("-")) {
grand_total = temp1 - temp2;
} else if (sign.equals("*")) {
grand_total = temp1 * temp2;
} else if (sign.equals("/")) {
grand_total = temp1 / temp2;
}
edit.setText(grand_total + "");
sign = "";
}
public void OnClear(View v) {
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
total = "";
}
}
经度:
07-13 18:45:32.831 25631-25631/com.example.shelbywallace.mycalc E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shelbywallace.mycalc, PID: 25631
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22533)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22533)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.shelbywallace.mycalc.MainActivity.OnCalculate(MainActivity.java:54)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5702)
at android.widget.TextView.performClick(TextView.java:10888)
at android.view.View$PerformClick.run(View.java:22533)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
java.lang.NumberFormatException: Invalid double: "" at
尝试将total
字符串初始化为"0"
而不是空字符串。否则,在尝试将任何内容转换为数字时会出错!
例如Double.parseDouble("");
会产生错误:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at Ideone.main(Main.java:12)
或者你可以创建一个方法来解析字符串,并为空字符串返回零:
public static int parseDouble(final String value) {
if (value.equals("")) {
return 0;
}
return Double.parseDouble(value);
}
然后你可以这样称呼它:
EditText edit = (EditText) findViewById(R.id.editText);
String str2 = edit.getText().toString();
temp2 = parseDouble(str2);