我正在尝试在android studio中设计一个简单的计算器,但每当我在模拟器上运行我的应用程序时,它就会崩溃。经过几次尝试后,它最终会打开并显示UI,但每当我点击一个按钮时,它就会崩溃。我是android studio的新手,所以我会很感激有关我的代码如何导致它崩溃的一些提示。
这是我的XML:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.calc.basiccalculator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:id="@+id/outputText"
android:layout_width="0dp"
android:layout_height="47dp"
android:text="0"
android:textAlignment="textEnd"
android:textSize="33dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="19dp"
android:layout_marginEnd="17dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/equals"
android:layout_marginStart="17dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="18dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="17dp" />
<Button
android:id="@+id/one"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="1"
android:onClick="pressOne"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/zero"
app:layout_constraintRight_toRightOf="@+id/four"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/four" />
<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="3"
android:onClick="pressThree"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/two"
app:layout_constraintRight_toRightOf="@+id/six"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/six"
app:layout_constraintTop_toTopOf="@+id/two" />
<Button
android:id="@+id/four"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="4"
android:onClick="pressFour"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/one"
app:layout_constraintRight_toRightOf="@+id/seven"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/seven" />
<Button
android:id="@+id/five"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="5"
android:onClick="pressFive"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/two"
app:layout_constraintRight_toRightOf="@+id/two"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/two" />
<Button
android:id="@+id/two"
android:layout_width="124dp"
android:layout_height="75dp"
android:onClick="pressTwo"
android:text="2"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/one"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
tools:layout_editor_absoluteY="433dp" />
<Button
android:id="@+id/six"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="6"
android:onClick="pressSix"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/three"
app:layout_constraintRight_toRightOf="@+id/nine"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/nine" />
<Button
android:id="@+id/eight"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="8"
android:onClick="pressEight"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/five"
app:layout_constraintRight_toRightOf="@+id/five"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/five" />
<Button
android:id="@+id/nine"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="9"
android:onClick="pressNine"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/six"
app:layout_constraintRight_toRightOf="@+id/subtract"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/subtract" />
<Button
android:id="@+id/seven"
android:layout_width="0dp"
android:layout_height="75dp"
android:onClick="pressSeven"
android:text="7"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/four"
app:layout_constraintRight_toRightOf="@+id/clear"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/clear" />
<Button
android:id="@+id/zero"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="0"
android:onClick="pressZero"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="@+id/one"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="16dp"
app:layout_constraintLeft_toLeftOf="@+id/one"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="CE"
android:onClick="clear"
app:layout_constraintRight_toLeftOf="@+id/plus"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/seven"
android:layout_marginStart="16dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:id="@+id/subtract"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="-"
android:onClick="pressSubtract"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/nine"
app:layout_constraintRight_toRightOf="@+id/divide"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/divide" />
<Button
android:id="@+id/plus"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="+"
android:onClick="pressPlus"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/multiply"
app:layout_constraintRight_toRightOf="@+id/multiply"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/multiply" />
<Button
android:id="@+id/multiply"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="*"
android:onClick="pressMultiply"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/eight"
app:layout_constraintRight_toRightOf="@+id/eight"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/eight" />
<Button
android:id="@+id/divide"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="/"
android:onClick="pressDivide"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toTopOf="@+id/subtract"
android:layout_marginStart="1dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toRightOf="@+id/plus"
android:layout_marginLeft="1dp"
android:layout_marginRight="16dp" />
<Button
android:id="@+id/backspace"
android:layout_width="0dp"
android:layout_height="75dp"
android:text="DEL"
android:onClick="pressBack"
tools:layout_constraintRight_creator="1"
app:layout_constraintRight_toRightOf="@+id/clear"
app:layout_constraintBaseline_toBaselineOf="@+id/plus"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="@+id/clear"
app:layout_constraintHorizontal_bias="0.0" />
<Button
android:id="@+id/equals"
android:layout_width="0dp"
android:layout_height="53dp"
android:text="="
android:onClick="pressEquals"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/divide"
app:layout_constraintRight_toRightOf="@+id/divide"
android:layout_marginTop="84dp"
tools:layout_constraintLeft_creator="1"
android:layout_marginBottom="75dp"
app:layout_constraintLeft_toLeftOf="@+id/divide"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
这是我的代码:
package com.calc.basiccalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String operation;
CharSequence num1;
CharSequence num2;
TextView t;
String n1;
String n2;
int number1;
int number2;
int output;
String out;
CharSequence temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.outputText);
operation = "";
num1 = "";
num2 = "";
n1 = "";
n2 = "";
number1 = 1;
number2 = 0;
output = 0;
out = "";
temp = "";
}
public void pressOne() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("1");
}
else {
t.append("1");
}
}
public void pressTwo() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("2");
}
else {
t.append("2");
}
}
public void pressThree() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("3");
}
else {
t.append("3");
}
}
public void pressFour() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("4");
}
else {
t.append("4");
}
}
public void pressFive() {
temp = t.getText();
if (temp == "0" && operation == "") {
t.clearComposingText();
t.setText("5");
}
else {
t.append("5");
}
}
public void pressSix() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("6");
}
else {
t.append("6");
}
}
public void pressSeven() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("7");
}
else {
t.append("7");
}
}
public void pressEight() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("8");
}
else {
t.append("8");
}
}
public void pressNine() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("9");
}
else {
t.append("9");
}
}
public void pressZero() {
temp = t.getText();
if (temp == "0") {
t.clearComposingText();
t.setText("0");
}
else {
t.append("0");
}
}
public void pressSubtract() {
num1 = t.getText();
t.clearComposingText();
operation = "-";
}
public void pressPlus() {
num1 = t.getText();
t.clearComposingText();
operation = "+";
}
public void pressMultiply() {
num1 = t.getText();
t.clearComposingText();
operation = "*";
}
public void pressDivide() {
num1 = t.getText();
t.clearComposingText();
operation = "/";
}
public void clear() {
t.clearComposingText();
}
public void pressEquals() {
if (operation == "+") {
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 + number2;
}
if (operation == "-") {
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 - number2;
}
if (operation == "*") {
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 * number2;
}
if (operation == "/") {
num2 = t.getText();
n1 = num1.toString();
n2 = num2.toString();
number1 = Integer.parseInt(n1);
number2 = Integer.parseInt(n2);
output = number1 / number2;
}
t.clearComposingText();
out = String.valueOf(output);
t.append(out);
operation = "";
}
}
答案 0 :(得分:2)
您可以通过调查日志输出轻松找到导致崩溃的错误,您可以在Android监视器窗口中找到该日志,如下图所示。
使用android:onClick属性在XML布局中分配方法时,该方法必须是公共的,并接受View作为其唯一参数。这在Android Developer reference。
中有解释代码中的方法都缺少View参数。
Car
答案 1 :(得分:0)
onClick方法声明应为
public void methodName(View v) {}
请将您的方法更改为:
public void pressOne(View v) {...}
public void pressTwo(View v) {...}
public void pressThree(View v) {...}
public void pressFour(View v) {...}
public void pressFive(View v) {...}