title: Whenever i click on playAgain button the app get crashed
body: <p><strong>Java</strong>
  I am new to Android Development and I am developing simple tic Tac Toe game. When I click on button the app get crashed. It is a cross and circle game, one who matches three continuous crosses or circles wins. After completion of a game the Textview and Button shows up. The aim is after tapping on play again button the game should restart. Here is the code in Java.</p>
  <pre><code>package com.example.tictactoe;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.GridLayout;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
  
      int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
  
      int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
  
      int activePlayer = 0;
  
      boolean gameActive = true;
  
      public void dropIn(View view) {
  
          ImageView counter = (ImageView) view;
  
          int tappedCounter = Integer.parseInt(counter.getTag().toString());
  
          if (gameState[tappedCounter] == 2 &amp;&amp; gameActive) {
  
              gameState[tappedCounter] = activePlayer;
  
              counter.setTranslationY(-1500);
  
              if (activePlayer == 0) {
  
                  counter.setImageResource(R.drawable.o);
                  activePlayer = 1;
  
              } else {
                  counter.setImageResource(R.drawable.x);
                  activePlayer = 0;
              }
  
              counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
  
              for (int[] winningPosition : winningPositions) {
  
                  if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &amp;&amp; gameState[winningPosition[1]] == gameState[winningPosition[2]] &amp;&amp; gameState[winningPosition[0]] != 2) {
  
                      // Somone has won!
  
                      gameActive = false;
  
                      String winner = &quot;&quot;;
  
                      if (activePlayer == 1) {
  
                          winner = &quot;Circle&quot;;
  
                      } else {
  
                          winner = &quot;Cross&quot;;
  
                      }
  
                      Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
  
                      TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
  
                      winnerTextView.setText(winner + &quot; has won!&quot;);
  
                      playAgainButton.setVisibility(View.VISIBLE);
  
                      winnerTextView.setVisibility(View.VISIBLE);
  
                  }
  
              }
          }
      }
  
      public void playAgain(View view) {
  
          Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
  
          TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
  
          playAgainButton.setVisibility(View.INVISIBLE);
  
          winnerTextView.setVisibility(View.INVISIBLE);
  
          GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
  
          for(int i=0; i&lt;gridLayout.getChildCount(); i++) {
  
              ImageView counter = (ImageView) gridLayout.getChildAt(i);
  
              counter.setImageDrawable(null);
  
          }
  
          for (int i=0; i&lt;gameState.length; i++) {
  
              gameState[i] = 2;
  
          }
  
          activePlayer = 0;
  
          gameActive = true;
  
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  }
  </code></pre>
  <p>Logcat when button is clicked.</p>
  <pre><code>2022-01-26 00:03:53.763 11101-11101/com.example.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.tictactoe, PID: 11101
      java.lang.IllegalStateException: Could not execute method for android:onClick
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
          at android.view.View.performClick(View.java:7448)
          at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
          at android.view.View.performClickInternal(View.java:7425)
          at android.view.View.access$3600(View.java:810)
          at android.view.View$PerformClick.run(View.java:28305)
          at android.os.Handler.handleCallback(Handler.java:938)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:223)
          at android.app.ActivityThread.main(ActivityThread.java:7656)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Method.invoke(Native Method)
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
          at android.view.View.performClick(View.java:7448) 
          at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
          at android.view.View.performClickInternal(View.java:7425) 
          at android.view.View.access$3600(View.java:810) 
          at android.view.View$PerformClick.run(View.java:28305) 
          at android.os.Handler.handleCallback(Handler.java:938) 
          at android.os.Handler.dispatchMessage(Handler.java:99) 
          at android.os.Looper.loop(Looper.java:223) 
          at android.app.ActivityThread.main(ActivityThread.java:7656) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
       Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
          at com.example.tictactoe.MainActivity.playAgain(MainActivity.java:95)
          at java.lang.reflect.Method.invoke(Native Method) 
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
          at android.view.View.performClick(View.java:7448) 
          at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
          at android.view.View.performClickInternal(View.java:7425) 
          at android.view.View.access$3600(View.java:810) 
          at android.view.View$PerformClick.run(View.java:28305) 
          at android.os.Handler.handleCallback(Handler.java:938) 
          at android.os.Handler.dispatchMessage(Handler.java:99) 
          at android.os.Looper.loop(Looper.java:223) 
          at android.app.ActivityThread.main(ActivityThread.java:7656) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
  </code></pre>
  <p>And here is the XML for button.</p>
  <pre><code>&lt;Button
          android:id=&quot;@+id/playAgainButton&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginStart=&quot;162dp&quot;
          android:layout_marginTop=&quot;5dp&quot;
          android:layout_marginEnd=&quot;162dp&quot;
          android:layout_marginBottom=&quot;21dp&quot;
          android:onClick=&quot;playAgain&quot;
          android:text=&quot;Play Again&quot;
          android:visibility=&quot;invisible&quot;
          app:layout_constraintBottom_toTopOf=&quot;@+id/gridLayout&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/winnerTextView&quot; /&gt;
  </code></pre>
  <p>I'm not sure how to go about fixing this, after tapping on Play Again button the game should restart. so hopefully someone can help. Thanks.</p>