我创建了一个计算器,但每次单击 *,/,+,- 两次时它都会崩溃。我知道它会崩溃,因为您不能进行两次除法/乘法运算,但是如何设置一个规则来检查乘法/除法是否已被单击一次?
这是代码:
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
Button add1, add2, add3, add4, add5, add6, add7, add8, add9,
plus, minus, divide, multiply, equal, add0, clear;
TextView Display, infobox;
String x;
double plusminus, firstDouble, secondDouble, totalDouble;
boolean plusClick, minusClick, multiplyClick, divideClick;
int checkMulti = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initialize();
}
void initialize() {
add0 = (Button) findViewById(R.id.b0);
// snipped cases for 1 to 8
add9 = (Button) findViewById(R.id.b9);
minus = (Button) findViewById(R.id.bSub);
equal = (Button) findViewById(R.id.bEquals);
plus = (Button) findViewById(R.id.bAdd);
clear = (Button) findViewById(R.id.bClear);
Display = (TextView) findViewById(R.id.tvResults);
divide = (Button) findViewById(R.id.bDivide);
multiply = (Button) findViewById(R.id.bMultiply);
add0.setOnClickListener(this);
// snipped cases for 1 to 8
add9.setOnClickListener(this);
minus.setOnClickListener(this);
equal.setOnClickListener(this);
plus.setOnClickListener(this);
Display.setOnClickListener(this);
divide.setOnClickListener(this);
multiply.setOnClickListener(this);
clear.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b0:
Display.setText(Display.getText() + "0");
break;
// ... snipped cases for 1 to 8
case R.id.b9:
Display.setText(Display.getText() + "9");
break;
case R.id.bAdd:
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
plusClick = true;
break;
case R.id.bSub:
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
minusClick = true;
break;
case R.id.bEquals:
secondDouble = (Double.parseDouble(String.valueOf(Display.getText())));
if (plusClick) {
totalDouble = firstDouble + secondDouble;
Display.setText((String.valueOf(totalDouble)));
firstDouble = 0;
secondDouble = 0;
plusClick = false;
}
if (minusClick) {
totalDouble = firstDouble - secondDouble;
Display.setText((String.valueOf(totalDouble)));
firstDouble = 0;
secondDouble = 0;
minusClick = false;
}
if (divideClick) {
totalDouble = firstDouble / secondDouble;
Display.setText((String.valueOf(totalDouble)));
firstDouble = 0;
secondDouble=0;
divideClick = false;
}
if (multiplyClick) {
totalDouble = firstDouble * secondDouble;
Display.setText((String.valueOf(totalDouble)));
firstDouble = 0;
secondDouble = 0;
multiplyClick = false;
}
break;
case R.id.bMultiply:
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
multiplyClick = true;
break;
case R.id.bDivide:
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
divideClick = true;
break;
case R.id.bClear:
Display.setText("");
multiplyClick = false;
divideClick = false;
plusClick = false;
minusClick = false;
break;
}
}
}
如果我没记错的话应该是这样的
case R.id.bMultiply:
if (Display.getText() != "") {
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
multiplyClick = true;
}
break;
case R.id.bDivide:
if (Display.getText() != null) {
firstDouble = (Double.parseDouble(String.valueOf(Display.getText())));
Display.setText("");
divideClick = true;
}
break;
LogCat(07-26 09:00:00 开始):
04:02.361: E/AndroidRuntime(5387): FATAL EXCEPTION: main
04:02.361: E/AndroidRuntime(5387): java.lang.NumberFormatException: Invalid double: ""
04:02.361: E/AndroidRuntime(5387): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
04:02.361: E/AndroidRuntime(5387): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
04:02.361: E/AndroidRuntime(5387): at java.lang.Double.parseDouble(Double.java:295)
04:02.361: E/AndroidRuntime(5387): at com.example.calculator.MainActivity.onClick(MainActivity.java:177)
04:02.361: E/AndroidRuntime(5387): at android.view.View.performClick(View.java:4162)
04:02.361: E/AndroidRuntime(5387): at android.view.View$PerformClick.run(View.java:17082)
04:02.361: E/AndroidRuntime(5387): at android.os.Handler.handleCallback(Handler.java:615)
04:02.361: E/AndroidRuntime(5387): at android.os.Handler.dispatchMessage(Handler.java:92)
04:02.361: E/AndroidRuntime(5387): at android.os.Looper.loop(Looper.java:137)
04:02.361: E/AndroidRuntime(5387): at android.app.ActivityThread.main(ActivityThread.java:4856)
04:02.361: E/AndroidRuntime(5387): at java.lang.reflect.Method.invokeNative(Native Method)
04:02.361: E/AndroidRuntime(5387): at java.lang.reflect.Method.invoke(Method.java:511)
04:02.361: E/AndroidRuntime(5387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04:02.361: E/AndroidRuntime(5387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04:02.361: E/AndroidRuntime(5387): at dalvik.system.NativeStart.main(Native Method)
04:10.429: I/Process(5387): Sending signal. PID: 5387 SIG: 9
19:01.891: D/libEGL(6671): loaded /system/lib/egl/libEGL_mali.so
19:01.901: D/libEGL(6671): loaded /system/lib/egl/libGLESv1_CM_mali.so
19:01.911: D/libEGL(6671): loaded /system/lib/egl/libGLESv2_mali.so
19:01.961: D/OpenGLRenderer(6671): Enabling debug mode 0
19:04.403: D/AndroidRuntime(6671): Shutting down VM
19:04.403: W/dalvikvm(6671): threadid=1: thread exiting with uncaught exception (group=0x418972a0)
19:04.433: E/AndroidRuntime(6671): FATAL EXCEPTION: main
19:04.433: E/AndroidRuntime(6671): java.lang.NumberFormatException: Invalid double: ""
19:04.433: E/AndroidRuntime(6671): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
19:04.433: E/AndroidRuntime(6671): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
19:04.433: E/AndroidRuntime(6671): at java.lang.Double.parseDouble(Double.java:295)
19:04.433: E/AndroidRuntime(6671): at com.example.calculator.MainActivity.onClick(MainActivity.java:168)
19:04.433: E/AndroidRuntime(6671): at android.view.View.performClick(View.java:4162)
19:04.433: E/AndroidRuntime(6671): at android.view.View$PerformClick.run(View.java:17082)
19:04.433: E/AndroidRuntime(6671): at android.os.Handler.handleCallback(Handler.java:615)
19:04.433: E/AndroidRuntime(6671): at android.os.Handler.dispatchMessage(Handler.java:92)
19:04.433: E/AndroidRuntime(6671): at android.os.Looper.loop(Looper.java:137)
19:04.433: E/AndroidRuntime(6671): at android.app.ActivityThread.main(ActivityThread.java:4856)
19:04.433: E/AndroidRuntime(6671): at java.lang.reflect.Method.invokeNative(Native Method)
19:04.433: E/AndroidRuntime(6671): at java.lang.reflect.Method.invoke(Method.java:511)
19:04.433: E/AndroidRuntime(6671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
19:04.433: E/AndroidRuntime(6671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
19:04.433: E/AndroidRuntime(6671): at dalvik.system.NativeStart.main(Native Method)
20:27.315: D/AndroidRuntime(6893): Shutting down VM
20:27.315: W/dalvikvm(6893): threadid=1: thread exiting with uncaught exception (group=0x418972a0)
20:27.325: E/AndroidRuntime(6893): FATAL EXCEPTION: main
20:27.325: E/AndroidRuntime(6893): java.lang.NumberFormatException: Invalid double: ""
20:27.325: E/AndroidRuntime(6893): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
20:27.325: E/AndroidRuntime(6893): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
20:27.325: E/AndroidRuntime(6893): at java.lang.Double.parseDouble(Double.java:295)
20:27.325: E/AndroidRuntime(6893): at com.example.calculator.MainActivity.onClick(MainActivity.java:168)
20:27.325: E/AndroidRuntime(6893): at android.view.View.performClick(View.java:4162)
20:27.325: E/AndroidRuntime(6893): at android.view.View$PerformClick.run(View.java:17082)
20:27.325: E/AndroidRuntime(6893): at android.os.Handler.handleCallback(Handler.java:615)
20:27.325: E/AndroidRuntime(6893): at android.os.Handler.dispatchMessage(Handler.java:92)
20:27.325: E/AndroidRuntime(6893): at android.os.Looper.loop(Looper.java:137)
20:27.325: E/AndroidRuntime(6893): at android.app.ActivityThread.main(ActivityThread.java:4856)
20:27.325: E/AndroidRuntime(6893): at java.lang.reflect.Method.invokeNative(Native Method)
20:27.325: E/AndroidRuntime(6893): at java.lang.reflect.Method.invoke(Method.java:511)
20:27.325: E/AndroidRuntime(6893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
20:27.325: E/AndroidRuntime(6893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
20:27.325: E/AndroidRuntime(6893): at dalvik.system.NativeStart.main(Native Method)
23:16.225: D/AndroidRuntime(7204): Shutting down VM
23:16.225: W/dalvikvm(7204): threadid=1: thread exiting with uncaught exception (group=0x418972a0)
23:16.245: E/AndroidRuntime(7204): FATAL EXCEPTION: main
23:16.245: E/AndroidRuntime(7204): java.lang.NumberFormatException: Invalid double: ""
23:16.245: E/AndroidRuntime(7204): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
23:16.245: E/AndroidRuntime(7204): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
23:16.245: E/AndroidRuntime(7204): at java.lang.Double.parseDouble(Double.java:295)
23:16.245: E/AndroidRuntime(7204): at com.example.calculator.MainActivity.onClick(MainActivity.java:176)
23:16.245: E/AndroidRuntime(7204): at android.view.View.performClick(View.java:4162)
23:16.245: E/AndroidRuntime(7204): at android.view.View$PerformClick.run(View.java:17082)
23:16.245: E/AndroidRuntime(7204): at android.os.Handler.handleCallback(Handler.java:615)
23:16.245: E/AndroidRuntime(7204): at android.os.Handler.dispatchMessage(Handler.java:92)
23:16.245: E/AndroidRuntime(7204): at android.os.Looper.loop(Looper.java:137)
23:16.245: E/AndroidRuntime(7204): at android.app.ActivityThread.main(ActivityThread.java:4856)
23:16.245: E/AndroidRuntime(7204): at java.lang.reflect.Method.invokeNative(Native Method)
23:16.245: E/AndroidRuntime(7204): at java.lang.reflect.Method.invoke(Method.java:511)
23:16.245: E/AndroidRuntime(7204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
23:16.245: E/AndroidRuntime(7204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
23:16.245: E/AndroidRuntime(7204): at dalvik.system.NativeStart.main(Native Method)