我想製作一個科學計算器(Android
應用程序),但會出現一些問題。我不知道Java
,但我瞭解代碼,因爲我知道C++
。 我已經修改了一個普通的計算器的代碼到一個科學計算器,但代碼似乎並沒有工作。 只有基本功能,如+
,-
,*
,/
工作,但我喜歡加Sin
,Cos
,Tan
,Sqrt
,Pow
,Exp
等功能似乎並不當我使用這些功能的應用程序崩潰的工作。 這是我的源代碼。我的Android應用程序不斷崩潰
MainActivity.java
package com.example.anant.scientificcalculator;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import static java.lang.Math.*;
import java.text.DecimalFormat;
import com.example.anant.scientificcalculator.databinding.MainActivityBinding;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private MainActivityBinding binding;
private static final char FACT = '!';
private static final char POWER = '^';
private static final String SQRT = "SQRT";
private static final String SIN = "SIN";
private static final String COS = "COS";
private static final String TAN = "TAN";
private static final String LOG = "LOG";
private static final char LEFTC = '(';
private static final char RIGHTC = ')';
private static final String EXP = "EXP";
private static final String PIE = "PIE";
private static final char ADD = '+';
private static final char SUBTRACT = '-';
private static final char MULTIPlY = '*';
private static final char DIVIDE = '/';
private char CURRENT_ACTION;
private String ADVANCE_ACTION;
private double valueOne = Double.NaN;
private double valueTwo;
private double valueTemp;
private DecimalFormat decimalFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
decimalFormat = new DecimalFormat("#.##########");
binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
binding.buttonDot.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonLeftC.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + "(");
}
});
binding.buttonRightC.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
binding.editText.setText(binding.editText.getText() + ")");
}
});
binding.buttonExp.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = EXP;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "EXP");
binding.editText.setText(null);
}
});
binding.buttonPie.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
computeCalculation();
ADVANCE_ACTION = PIE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "PIE");
binding.editText.setText(null);
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADD;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPlY;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVIDE;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonFact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = FACT;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "!");
binding.editText.setText(null);
}
});
binding.buttonPow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = POWER;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "^");
binding.editText.setText(null);
}
});
binding.buttonSqrt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SQRT;
binding.infoTextView.setText("SQRT(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonSine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = SIN;
binding.infoTextView.setText("SIN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonCosine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = COS;
binding.infoTextView.setText("COS(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonTangent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = TAN;
binding.infoTextView.setText("TAN(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonLog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
ADVANCE_ACTION = LOG;
binding.infoTextView.setText("LOG(" + decimalFormat.format(valueOne) + ")");
binding.editText.setText(null);
}
});
binding.buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length()-1));
}
else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if(!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if(CURRENT_ACTION == ADD)
valueOne = this.valueOne + valueTwo;
else if(CURRENT_ACTION == SUBTRACT)
valueOne = this.valueOne - valueTwo;
else if(CURRENT_ACTION == MULTIPlY)
valueOne = this.valueOne * valueTwo;
else if(CURRENT_ACTION == DIVIDE)
valueOne = this.valueOne/valueTwo;
else if(CURRENT_ACTION == FACT)
{
valueTemp = this.valueOne;
int i,valueThree=1;
for(i=(int) valueTemp;i>0;i--) //type conversion
{
valueThree *= i;
}
valueOne = valueThree;
}
else if(CURRENT_ACTION == POWER) {
valueTemp = this.valueOne;
valueTemp = Math.pow(valueOne, valueTwo);
valueOne = valueTemp;
}
else if(ADVANCE_ACTION == SQRT) {
valueTemp = this.valueOne;
valueOne = Math.sqrt(valueTemp);
}
else if(ADVANCE_ACTION == SIN) {
valueTemp = this.valueOne;
valueOne = Math.sin(valueTemp);
}
else if(ADVANCE_ACTION == COS) {
valueTemp = this.valueOne;
valueOne = Math.cos(valueTemp);
}
else if(ADVANCE_ACTION == TAN) {
valueTemp = this.valueOne;
valueOne = Math.tan(valueTemp);
}
else if(ADVANCE_ACTION == LOG) {
valueTemp = this.valueOne;
valueOne = Math.log(valueTemp);
}
else if(ADVANCE_ACTION == PIE) {
valueTemp = this.valueOne;
valueTemp = Math.PI;
valueOne = valueTemp;
}
else if(ADVANCE_ACTION == EXP) {
valueTemp = this.valueOne;
valueTemp = Math.E;
valueOne = valueTemp;
}
}
else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
}
catch (Exception e){}
}
}
}
誰能告訴我爲什麼我的應用程序不斷崩潰?
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anant.scientificcalculator.MainActivity">
<TextView
android:id="@+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:hint="0"
android:inputType="numberDecimal"
android:lines="2"
android:maxLines="2"
android:textAlignment="textEnd"
android:textColor="@android:color/black"
android:textSize="40sp" />
<Button
android:id="@+id/buttonFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="@string/buttonFact"
android:textSize="20sp" />
<Button
android:id="@+id/buttonPow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonFact"
android:text="@string/buttonPow"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSqrt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:layout_toRightOf="@id/buttonPow"
android:text="@string/buttonSqrt"
android:textSize="20sp" />
<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/buttonSqrt"
android:layout_below="@id/editText"
android:text="@string/buttonClear"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFact"
android:text="@string/buttonSine"
android:textSize="20sp" />
<Button
android:id="@+id/buttonCosine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonPow"
android:layout_toRightOf="@id/buttonSine"
android:text="@string/buttonCosine"
android:textSize="20sp" />
<Button
android:id="@+id/buttonTangent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSqrt"
android:layout_toRightOf="@id/buttonCosine"
android:text="@string/buttonTangent"
android:textSize="20sp" />
<Button
android:id="@+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonClear"
android:layout_toRightOf="@id/buttonTangent"
android:text="@string/buttonLog"
android:textSize="20sp" />
<Button
android:id="@+id/buttonLeftC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSine"
android:text="@string/buttonLeftC"
android:textSize="20sp" />
<Button
android:id="@+id/buttonRightC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonCosine"
android:layout_toRightOf="@id/buttonLeftC"
android:text="@string/buttonRightC"
android:textSize="20sp" />
<Button
android:id="@+id/buttonExp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTangent"
android:layout_toRightOf="@id/buttonRightC"
android:text="@string/buttonExp"
android:textSize="20sp" />
<Button
android:id="@+id/buttonPie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLog"
android:layout_toRightOf="@id/buttonExp"
android:text="@string/buttonPie"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLeftC"
android:text="@string/buttonSeven"
android:textSize="20sp" />
<Button
android:id="@+id/buttonEight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonRightC"
android:layout_toRightOf="@id/buttonSeven"
android:text="@string/buttonEight"
android:textSize="20sp" />
<Button
android:id="@+id/buttonNine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonExp"
android:layout_toRightOf="@id/buttonEight"
android:text="@string/buttonNine"
android:textSize="20sp" />
<Button
android:id="@+id/buttonFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSeven"
android:text="@string/buttonFour"
android:textSize="20sp" />
<Button
android:id="@+id/buttonFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonEight"
android:layout_toRightOf="@id/buttonFour"
android:text="@string/buttonFive"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonNine"
android:layout_toRightOf="@id/buttonFive"
android:text="@string/buttonSix"
android:textSize="20sp" />
<Button
android:id="@+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFour"
android:text="@string/buttonOne"
android:textSize="20sp" />
<Button
android:id="@+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonFive"
android:layout_toRightOf="@id/buttonOne"
android:text="@string/buttonTwo"
android:textSize="20sp" />
<Button
android:id="@+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSix"
android:layout_toRightOf="@id/buttonTwo"
android:text="@string/buttonThree"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonOne"
android:text="@string/buttonDot"
android:textSize="20sp" />
<Button
android:id="@+id/buttonZero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/buttonTwo"
android:layout_toRightOf="@id/buttonDot"
android:text="@string/buttonZero"
android:textSize="20sp" />
<Button
android:id="@+id/buttonEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/buttonNine"
android:layout_below="@id/buttonThree"
android:text="@string/buttonEqual"
android:textSize="20sp" />
<Button
android:id="@+id/buttonDivide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonNine"
android:layout_toRightOf="@id/buttonNine"
android:text="@string/buttonDIvide"
android:textSize="20sp" />
<Button
android:id="@+id/buttonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonSix"
android:layout_toRightOf="@id/buttonSix"
android:text="@string/buttonMultiply"
android:textSize="20sp" />
<Button
android:id="@+id/buttonSubtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonThree"
android:layout_toRightOf="@id/buttonThree"
android:text="@string/buttonSubtract"
android:textSize="20sp" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/buttonEqual"
android:layout_toRightOf="@id/buttonEqual"
android:text="@string/buttonAdd"
android:textSize="20sp" />
</RelativeLayout>
</layout>
發佈您的logcat錯誤msg – sravs
你是什麼意思 –
請錯誤日誌? – Aryan