title: Android application keeps crashing (Android Studio)
body: <p>I am trying to design a simple calculator in android studio, but whenever i run my app on the emulator, it crashes. After a few tries, it eventually opens and shows the UI, but whenever i click on a button, it crashes. I'm new to android studio, so some tips on how my code is causing it to crash would be appreciated.</p>
  
  <p>This is my XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;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"&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
      &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;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" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>And this is my Code :</p>
  
  <pre><code>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" &amp;&amp; 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 = "";
  
    }
  }
  </code></pre>