我一直在开发Android工作室的基本Calculator
应用。我已经深入了解它,我对目前的进展感到满意。我现在面临的问题是,当我运行Calculator
应用程序时,每当我在纵向模式下输入数字并切换到横向模式并尝试添加数字或对其执行任何操作时,应用程序崩溃。我使用onSaveInstanceState
方法保存数据,使用onRestoreInstanceState
方法检索数据。我查看了有关如何使用这两种方法的文档,我确信我已正确实现它们。任何帮助将不胜感激。
我已将代码粘贴到下方。
package com.mohamedali.calculatorapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText moeresult;
private EditText moenewNumber;
private TextView displayOperation;
//Comment: Variables to hold the operands and type of calculations
private Double operand1 = null;
private String pendingOperation = " = ";
public static final String STATE_PENDING_OPERATION = "Pending Operation";
public static final String STATE_OPERAND1 = "Operand 1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moeresult = (EditText) findViewById(R.id.result);
moenewNumber = (EditText) findViewById(R.id.NewNumber);
displayOperation = (TextView) findViewById(R.id.Operation);
Button button0 = (Button) findViewById(R.id.button0);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
Button button5 = (Button) findViewById(R.id.button5);
Button button6 = (Button) findViewById(R.id.button6);
Button button7 = (Button) findViewById(R.id.button7);
Button button8 = (Button) findViewById(R.id.button8);
Button button9 = (Button) findViewById(R.id.button9);
Button buttonDecimal = (Button) findViewById(R.id.buttonDecimal);
Button buttonMultiply = (Button) findViewById(R.id.buttonMultiply);
Button buttonDivide = (Button) findViewById(R.id.buttonDivide);
Button buttonMinus = (Button) findViewById(R.id.buttonMinus);
Button buttonPlus = (Button) findViewById(R.id.buttonPlus);
Button buttonEqual = (Button) findViewById(R.id.buttonEqual);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
moenewNumber.append(b.getText().toString());
}
};
button0.setOnClickListener(listener);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
button5.setOnClickListener(listener);
button6.setOnClickListener(listener);
button7.setOnClickListener(listener);
button8.setOnClickListener(listener);
button9.setOnClickListener(listener);
buttonDecimal.setOnClickListener(listener);
View.OnClickListener opListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Button b = (Button) view;
String op = b.getText().toString();
String value = moenewNumber.getText().toString();
try {
Double doublevalue = Double.valueOf(value);
performOperation(doublevalue, op);
} catch (NumberFormatException e) {
moenewNumber.setText("");
}
pendingOperation = op;
displayOperation.setText(pendingOperation);
}
};
buttonEqual.setOnClickListener(opListener);
buttonMultiply.setOnClickListener(opListener);
buttonMinus.setOnClickListener(opListener);
buttonDivide.setOnClickListener(opListener);
buttonPlus.setOnClickListener(opListener);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(STATE_PENDING_OPERATION, pendingOperation);
if (operand1 != null) {
outState.putDouble(STATE_PENDING_OPERATION, operand1);
}
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
pendingOperation = savedInstanceState.getString(STATE_PENDING_OPERATION);
operand1 = savedInstanceState.getDouble(STATE_OPERAND1);
displayOperation.setText(pendingOperation);
}
private void performOperation(Double value, String operation) {
// first thing is that it checks if first value is null
if (null == operand1) {
operand1 = value;
} else {
if (pendingOperation.equals(" = ")) {
pendingOperation = operation;
}
switch (pendingOperation) {
case "=":
operand1 = value;
break;
case "/":
if (value == 0) {
operand1 = 0.0;
} else {
operand1 /= value;
}
break;
case "*":
operand1 *= value;
break;
case "-":
operand1 -= value;
break;
case "+":
operand1 += value;
break;
}
}
// Displays the result
moeresult.setText(operand1.toString());
// It then clears it
moenewNumber.setText("");
}
}