title: App Suddenly Hangs - Build App for Random number subtraction
body: <p>I am building app for my daughter for math
  Everything is working great - add, multiply, divide, between, ascending, etc</p>
  
  <p>but in subtract it loads first time, for second time app get's hang - I have to directly exits it, clear it from ram and restart it every time</p>
  
  <p>log cat is not showing any error or evening warning,
  please guide me - I am beginner (actually first app)</p>
  
  <p>my code</p>
  
  <pre><code>    package youtube.computers.mohammedi.kidsmath;
  
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.TextView;
      import android.widget.Toast;
      import com.google.android.gms.ads.AdRequest.Builder;
      import com.google.android.gms.ads.AdView;
      import java.util.Random;
  
      public class Subtract extends GlobalOverride implements OnClickListener {
          int ans;
          Button btn1;
          Button btn2;
          Button btn3;
          Button btn4;
          int max;
          int n1;
          int n2;
          Random f16r;
          int temp;
          TextView txt_add;
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView((int) R.layout.activity_subtract);
          setTitle("Subtraction");
          this.btn1 =  findViewById(R.id.btn1);
          this.btn2 =  findViewById(R.id.btn2);
          this.btn3 =  findViewById(R.id.btn3);
          this.btn4 =  findViewById(R.id.btn4);
          this.txt_add = (TextView) findViewById(R.id.txt_ascen);
          this.btn1.setOnClickListener(this);
          this.btn2.setOnClickListener(this);
          this.btn3.setOnClickListener(this);
          this.btn4.setOnClickListener(this);
          this.max = getSharedPreferences("Math", 0).getInt("add", 10);
          ((AdView) findViewById(R.id.adView)).loadAd(new Builder().build());
          generate();
      }
  
      public void onClick(View v) {
          this.ans = Integer.parseInt((String) ((Button) v).getText());
          if (this.ans == this.temp) {
              generate();
          } else {
              Toast.makeText(this, "Incorrect", Toast.LENGTH_SHORT).show();
          }
      }
  
      public void generate() {
          this.n1 = GlobalOverride.getRand(1, this.max);
          this.n2 = GlobalOverride.getRand(1, this.max);
          if(n2&gt;n1 &amp;&amp; max&lt;=20){
              generate();
          }
          this.temp = this.n1 - this.n2;
          this.txt_add.setText(this.n1 + " - " + this.n2 + " = ?");
          int[] answer = new int[4];
          int count = 0;
          while (count &lt;= 3) {
              int random_integer = GlobalOverride.getRand(this.temp - 2, this.temp + 2);
              if (!exists(random_integer, answer)) {
                  answer[count] = random_integer;
                  count++;
              }
          }
          if (!exists(this.temp, answer)) {
              answer[GlobalOverride.getRand(0, 3)] = this.temp;
          }
          this.btn1.setText(String.valueOf(answer[0]));
          this.btn2.setText(String.valueOf(answer[1]));
          this.btn3.setText(String.valueOf(answer[2]));
          this.btn4.setText(String.valueOf(answer[3]));
      }
  
      public boolean exists(int number, int[] array) {
          if (number == -1) {
              return true;
          }
          for (int i : array) {
              if (number == i) {
                  return true;
              }
          }
          return false;
          }
      }
  </code></pre>
  
  <p><strong>GlobalOverride Class.</strong></p>
  
  <pre><code>package youtube.computers.mohammedi.kidsmath;
  
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import java.net.InetAddress;
  
  public class GlobalOverride extends AppCompatActivity {
  
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_levels) {
              startActivity(new Intent(this, Levels.class));
          }
          if (id == R.id.action_contact) {
              startActivity(new Intent(this, Contact.class));
          }
          return super.onOptionsItemSelected(item);
      }
  
      protected static int getRand(int lower, int upper) {
          return ((int) (Math.random() * ((double) ((upper + 1) - lower)))) + lower;
      }
  
      public boolean isInternetAvailable() {
          try {
              if (InetAddress.getByName("google.com").equals("")) {
                  return false;
              }
              return true;
          } catch (Exception e) {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>Layout Output</p>
  
  <p><a href="https://i.stack.imgur.com/SyIBF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SyIBF.png" alt="enter image description here"></a></p>