现在我的计算器应用程序出现了一个新问题,因此我可以打开应用程序,然后单击一系列按钮,例如 1 x 3 =
当我单击 ButtonEquals 按钮时,我得到“不幸的是,计算器已停止”。我似乎无法弄清楚为什么。
原始线程:在 Eclipse AVD 中,我收到“不幸的是,计算器已停止”。API 级别 16(修复了较早的问题)
MainActivity.Java
package com.papps.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num1 = 90;
int num2 = 90;
int op = 0;
int ans = 0;
TextView answer;
Button ButtonEquals;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (TextView) findViewById(R.id.answer);
ButtonEquals = (Button) findViewById(R.id.ButtonEquals);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//ONE
public void none(View v) {
if(num1 == 90) {
num1 = 1;
}
else if(num2 == 90) {
num2 = 1;
}
}
//TWO
public void ntwo(View v) {
if(num1 == 90) {
num1 = 2;
}
else if(num2 == 90) {
num2 = 2;
}
}
//THREE
public void nthree(View v) {
if(num1 == 90) {
num1 = 3;
}
else if(num2 == 90) {
num2 = 3;
}
}
//FOUR
public void nfour(View v) {
if(num1 == 90) {
num1 = 4;
}
else if(num2 == 90) {
num2 = 4;
}
}
//FIVE
public void nfive(View v) {
if(num1 == 90) {
num1 = 5;
}
else if(num2 == 90) {
num2 = 5;
}
}
//SIX
public void nsix(View v) {
if(num1 == 90) {
num1 = 6;
}
else if(num2 == 90) {
num2 = 6;
}
}
//SEVEN
public void nseven(View v) {
if(num1 == 90) {
num1 = 7;
}
else if(num2 == 90) {
num2 = 7;
}
}
//EIGHT
public void neight(View v) {
if(num1 == 90) {
num1 = 8;
}
else if(num2 == 90) {
num2 = 8;
}
}
//NINE
public void nnine(View v) {
if(num1 == 90) {
num1 = 9;
}
else if(num2 == 90) {
num2 = 9;
}
}
//ZERO
public void nzero(View v) {
if(num1 == 90) {
num1 = 0;
}
else if(num2 == 90) {
num2 = 0;
}
}
//operators
//MULTIPLY
public void multiply(View v) {
op = 1;
}
//Divide
public void divide(View v) {
op = 2;
}
//Add
public void add(View v) {
op = 3;
}
//Subtract
public void subtract(View v) {
op = 4;
}
//EQUALS
//TextView answer = (TextView) findViewById(R.id.answer);
//Button ButtonEquals = (Button) findViewById(R.id.ButtonEquals);
public void equals(View v) {
switch(op) {
case 1 :
answer.setText(num1 * num2);
case 2 :
answer.setText(num1 / num2);
case 3 :
answer.setText(num1 + num2);
case 4 :
answer.setText(num1 - num2);
}
}
}
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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="97dp"
android:layout_marginTop="16dp"
android:text="@string/app_name"
tools:context=".MainActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="14dp"
android:layout_marginTop="15dp"
android:text="1"
android:onClick = "none" />
<Button
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginLeft="15dp"
android:text="2"
android:onClick = "ntwo" />
<Button
android:id="@+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button2"
android:layout_alignBottom="@+id/Button2"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/Button2"
android:text="3"
android:onClick = "nthree" />
<Button
android:id="@+id/Button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:text="4"
android:onClick = "nfour" />
<Button
android:id="@+id/Button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button4"
android:layout_alignBottom="@+id/Button4"
android:layout_alignLeft="@+id/Button2"
android:text="5"
android:onClick = "nfive" />
<Button
android:id="@+id/Button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Button5"
android:layout_alignBottom="@+id/Button5"
android:layout_alignLeft="@+id/Button3"
android:text="6"
android:onClick = "nsix" />
<Button
android:id="@+id/Button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button4"
android:layout_centerVertical="true"
android:text="7"
android:onClick = "nseven" />
<Button
android:id="@+id/Button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/Button3"
android:text="8"
android:onClick = "neight" />
<Button
android:id="@+id/Button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button6"
android:layout_centerVertical="true"
android:text="9"
android:onClick = "nnine" />
<Button
android:id="@+id/Button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Button8"
android:layout_below="@+id/Button8"
android:layout_marginTop="29dp"
android:text="0"
android:onClick = "nzero" />
<Button
android:id="@+id/ButtonM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="X"
android:onClick = "multiply" />
<Button
android:id="@+id/ButtonD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/Button7"
android:text="/"
android:onClick = "divide" />
<Button
android:id="@+id/ButtonS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView1"
android:text="-"
android:onClick = "subtract" />
<Button
android:id="@+id/ButtonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/ButtonS"
android:text="+"
android:onClick = "add" />
<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ButtonD"
android:layout_marginBottom="24dp"
android:layout_toRightOf="@+id/ButtonD"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/ButtonEquals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/answer"
android:layout_alignBottom="@+id/answer"
android:layout_toLeftOf="@+id/Button0"
android:text="="
android:onClick = "equals" />
</RelativeLayout>