title: Android Image Button to tic tac toe
body: <p>Currently having an issue having an image button open to another activity when we try the app closes. What we are trying to create is a tic tac toe game that has a splash screen, main activity, and new activity that is the actual game. The splash screen opens to the main but when we try to open the main activity to the board game it closes on us. Any advice?</p>
  <p>Here is the current code:
  Activity1 :
  package com.example.tictactoe_game;</p>
  <pre><code>    import androidx.appcompat.app.AppCompatActivity;
      
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.ImageButton;
      
      
      public class MainActivity extends AppCompatActivity {
          ImageButton newActivity;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
             newActivity = (ImageButton) findViewById(R.id.imageButton_play);
      
              newActivity.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent intent = new Intent(MainActivity.this, GameBoard.class);
                      startActivity(intent);
      
                  }
              });
          }
      
      }
  </code></pre>
  <p>Activity 2:
  package com.example.tictactoe_game;</p>
  <pre><code>import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class GameBoard extends AppCompatActivity implements View.OnClickListener {
      private Button[][] buttons = new Button[3][3];
      private boolean player1Turn = true;
      private int roundCount;
  
      private int player1Points;
      private int player2Points;
  
      private TextView textViewPlayer1;
      private TextView textViewPlayer2;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_game_board);
  
  
          textViewPlayer1 = findViewById(R.id.text_viewp1);
          textViewPlayer2 = findViewById(R.id.text_viewp2);
  
          for (int i = 0; i &lt; 3; i++) {
              for (int j = 0; j &lt; 3; j++) {
                  String buttonID = &quot;button_&quot; + i + j;
                  int resID = getResources().getIdentifier(buttonID, &quot;id&quot;, getPackageName());
                  buttons[i][j] = findViewById(resID);
                  buttons[i][j].setOnClickListener(this);
              }
  
          }
  
      }
      @Override
      public void onClick(View v) {
          if (!((Button) v).getText().toString().equals(&quot;&quot;)) {
              return;
          }
          if (player1Turn) {
              ((Button) v).setText(&quot;X&quot;);
          } else {
              ((Button) v).setText(&quot;O&quot;);
          }
          roundCount++;
          if (checkForWin()) {
              if (player1Turn) {
                  player1Wins();
              } else {
                  player2Wins();
              }
          } else if (roundCount == 9) {
              draw();
          } else {
              player1Turn = !player1Turn;
          }
      }
  
      private boolean checkForWin() {
          String[][] field = new String[3][3];
          for (int i = 0; i &lt; 3; i++) {
              for (int j = 0; j &lt; 3; j++) {
                  field[i][j] = buttons[i][j].getText().toString();
              }
          }
          for (int i = 0; i &lt; 3; i++) {
              if (field[i][0].equals(field[i][1])
                      &amp;&amp; field[i][0].equals(field[i][2])
                      &amp;&amp; !field[i][0].equals(&quot;&quot;)) {
                  return true;
              }
          }
          for (int i = 0; i &lt; 3; i++) {
              if (field[0][i].equals(field[1][i])
                      &amp;&amp; field[0][i].equals(field[2][i])
                      &amp;&amp; !field[0][i].equals(&quot;&quot;)) {
                  return true;
              }
          }
          if (field[0][0].equals(field[1][1])
                  &amp;&amp; field[0][0].equals(field[2][2])
                  &amp;&amp; !field[0][0].equals(&quot;&quot;)) {
              return true;
          }
          if (field[0][2].equals(field[1][1])
                  &amp;&amp; field[0][2].equals(field[2][0])
                  &amp;&amp; !field[0][2].equals(&quot;&quot;)) {
              return true;
          }
          return false;
      }
  
      private void player1Wins() {
          player1Points++;
          Toast.makeText(this, &quot;Player 1 Wins!&quot;, Toast.LENGTH_SHORT).show();
          updatePointsText();
          resetBoard();
      }
  
      private void player2Wins() {
          player2Points++;
          Toast.makeText(this, &quot;Player 2 Wins!&quot;, Toast.LENGTH_SHORT).show();
          updatePointsText();
          resetBoard();
      }
  
      private void draw() {
          Toast.makeText(this, &quot;Draw!&quot;, Toast.LENGTH_SHORT).show();
          resetBoard();
      }
  
      private void updatePointsText() {
          textViewPlayer1.setText(&quot;Player 1: &quot; + player1Points);
          textViewPlayer2.setText(&quot;Player 2: &quot; + player2Points);
      }
  
      private void resetBoard() {
          for (int i = 0; i &lt; 3; i++) {
              for (int j = 0; j &lt; 3; j++) {
                  buttons[i][j].setText(&quot;&quot;);
              }
          }
          roundCount = 0;
          player1Turn = true;
      }
  }
  
          
  </code></pre>