title: Android studio Activity not open when i put following piece of code
body: <p>This is my code:</p>
  
  <pre><code>    package com.appsoft23.bestmathtrainerforkids.free;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  import java.util.Random;
  
  public class Add extends AppCompatActivity {
  
      Button startButton;
      ArrayList&lt;Integer&gt; answers = new ArrayList&lt;Integer&gt;();
      int correctLocation;
  
      public void start(View view){
  
          startButton.setVisibility(View.INVISIBLE);
      }
  
          @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.add);
  
          startButton = (Button) findViewById(R.id.startButton);
          TextView addTextView = (TextView) findViewById(R.id.addTextView);
          Button button4 = (Button) findViewById(R.id.button4);
          Button button5 = (Button) findViewById(R.id.button5);
          Button button6 = (Button) findViewById(R.id.button6);
          Button button7 = (Button) findViewById(R.id.button7);
  
  
          Random rand = new Random();
          int h = rand.nextInt(21);
          int t = rand.nextInt(21);
          addTextView.setText(Integer.toString(h) + "+" + Integer.toString(t));
          correctLocation = rand.nextInt(4);
          int wrongAnswer;
          for(int i=0; i&lt;4; i++){
              if (i == correctLocation){
                  answers.add(h+t);
              } else {
                  wrongAnswer = rand.nextInt(42);
                  while (wrongAnswer == h+t){
                      wrongAnswer = rand.nextInt(42);
                  }
                  answers.add(wrongAnswer);
              }
          }
          button4.setText(Integer.toString(answers.get(4)));
          button5.setText(Integer.toString(answers.get(5)));
          button6.setText(Integer.toString(answers.get(6)));
          button7.setText(Integer.toString(answers.get(7)));
  
      }
  
  
  }
  </code></pre>
  
  <p>it gives following error:</p>
  
  <blockquote>
    <p>E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.appsoft23.bestmathtrainerforkids.free, PID: 3095
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appsoft23.bestmathtrainerforkids.free/com.appsoft23.bestmathtrainerforkids.free.Add}: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                       Caused by: java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
                          at java.util.ArrayList.get(ArrayList.java:411)
                          at com.appsoft23.bestmathtrainerforkids.free.Add.onCreate(Add.java:53)
                          at android.app.Activity.performCreate(Activity.java:6664)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6077) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) </p>
  </blockquote>
  
  <p>Please Help :) thanks</p>