title: Tictac toe game keeps stopping with logcat error java.lang.IllegalStateException:Could not execute method for android:onClick
body: <p>When I play the game it causes an error after some time and the window closes, App keep stopping.<br />
  Logcat for the game is</p>
  <pre><code>    Process: com.example.tictactoe, PID: 12663
  java.lang.IllegalStateException: Could not execute method for android:onClick
      at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
      at android.view.View.performClick(View.java:7448)
      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:28309)
      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:7698)
      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:952)
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invoke(Native Method)
      at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
      at android.view.View.performClick(View.java:7448) 
      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:28309) 
      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:7698) 
      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:952) 
   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
      at com.example.tictactoe.MainActivity.playertap(MainActivity.java:32)
      at java.lang.reflect.Method.invoke(Native Method) 
      at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
      at android.view.View.performClick(View.java:7448) 
      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:28309) 
      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:7698) 
      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:952) 
  </code></pre>
  <p>XML Code for the game is</p>
  <pre><code>
      &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;TextView
          android:id=&quot;@+id/textView&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginTop=&quot;10dp&quot;
          android:fontFamily=&quot;cursive&quot;
          android:padding=&quot;10dp&quot;
          android:text=&quot;@string/heading&quot;
          android:textSize=&quot;34sp&quot;
          android:textStyle=&quot;bold&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.498&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintVertical_bias=&quot;0.0&quot; /&gt;
  
      &lt;ImageView
          android:id=&quot;@+id/imageView&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;0dp&quot;
          android:contentDescription=&quot;@string/main_grid&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.0&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;
          app:layout_constraintVertical_bias=&quot;1.0&quot;
          app:srcCompat=&quot;@drawable/grid&quot; /&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;420dp&quot;
          android:orientation=&quot;vertical&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;@+id/imageView&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/textView&quot;&gt;
  
          &lt;LinearLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
              android:orientation=&quot;horizontal&quot;&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView1&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:contentDescription=&quot;@string/o_value&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;1&quot;
                  android:padding=&quot;20sp&quot;
                   /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView2&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;2&quot;
                  android:padding=&quot;20sp&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView3&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;3&quot;
                  android:contentDescription=&quot;TODO&quot;
                  android:padding=&quot;20sp&quot; /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
              android:orientation=&quot;horizontal&quot;&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView4&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;4&quot;
                  android:padding=&quot;20sp&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView5&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;5&quot;
                  android:padding=&quot;20sp&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView6&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;6&quot;
                  android:padding=&quot;20sp&quot; /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;LinearLayout
              android:layout_width=&quot;match_parent&quot;
              android:layout_height=&quot;match_parent&quot;
              android:layout_weight=&quot;1&quot;
  
              android:orientation=&quot;horizontal&quot;&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView7&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;7&quot;
                  android:padding=&quot;20sp&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView8&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick=&quot;playertap&quot;
                  android:tag=&quot;8&quot;
                  android:padding=&quot;20sp&quot; /&gt;
  
              &lt;ImageView
                  android:id=&quot;@+id/imageView9&quot;
                  android:layout_width=&quot;match_parent&quot;
                  android:layout_height=&quot;match_parent&quot;
                  android:layout_weight=&quot;1&quot;
                  android:onClick =&quot;playertap&quot;
                  android:tag=&quot;9&quot;
                  android:padding=&quot;20sp&quot; /&gt;
          &lt;/LinearLayout&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;TextView
          android:id=&quot;@+id/status&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginBottom=&quot;15sp&quot;
          android:text=&quot;@string/status&quot;
          android:textSize=&quot;18sp&quot;
          android:textStyle=&quot;italic&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintEnd_toEndOf=&quot;parent&quot;
          app:layout_constraintStart_toStartOf=&quot;parent&quot;
          app:layout_constraintTop_toBottomOf=&quot;@+id/linearLayout&quot; /&gt;
  
  &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  like so
  </code></pre>
  <p><em><strong>mainactivity java code:</strong></em></p>
  <pre><code>
  
  
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  import androidx.appcompat.app.AppCompatActivity;
  
  public class MainActivity extends AppCompatActivity {
      boolean gameActive = true;
      // Player representation
      // 0 - X
      // 1 - O
      int activePlayer = 0;
      int[] gameState = {2, 2 , 2, 2, 2, 2, 2, 2, 2};
      //    State meanings:
      //    0 - X
      //    1 - O
      //    2 - Null
      int[][] winPositions = {{0,1,2}, {3,4,5}, {6,7,8},
              {0,3,6}, {1,4,7}, {2,5,8},
              {0,4,8}, {2,4,6}};
      public void playertap(View view){
          ImageView img = (ImageView) view;
          int tappedImage = Integer.parseInt(img.getTag().toString());
          if(!gameActive){
              gameReset(view);
          }
          if(gameState[tappedImage] == 2) {
              gameState[tappedImage] = activePlayer;
              img.setTranslationY(-1000f);
              if (activePlayer == 0) {
                  img.setImageResource(R.drawable.x);
                  activePlayer = 1;
                  TextView status = findViewById(R.id.status);
                  status.setText(&quot;O's Turn - Tap to play&quot;);
              } else {
                  img.setImageResource(R.drawable.o);
                  activePlayer = 0;
                  TextView status = findViewById(R.id.status);
                  status.setText(&quot;X's Turn - Tap to play&quot;);
              }
              img.animate().translationYBy(1000f).setDuration(300);
          }
          // Check if any player has won
          for(int[] winPosition: winPositions){
              if(gameState[winPosition[0]] == gameState[winPosition[1]] &amp;&amp;
                      gameState[winPosition[1]] == gameState[winPosition[2]] &amp;&amp;
                      gameState[winPosition[0]]!=2){
                  // Somebody has won! - Find out who!
                  String winnerStr;
                  gameActive = false;
                  if(gameState[winPosition[0]] == 0){
                      winnerStr = &quot;X has won&quot;;
                  }
                  else{
                      winnerStr = &quot;O has won&quot;;
                  }
                  // Update the status bar for winner announcement
                  TextView status = findViewById(R.id.status);
                  status.setText(winnerStr);
  
              }
  
  
  
          }
  
      }
  
      public void gameReset(View view) {
          gameActive = true;
          activePlayer = 0;
          for(int i=0; i&lt;gameState.length; i++){
              gameState[i] = 2;
          }
          ((ImageView)findViewById(R.id.imageView9)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView1)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView2)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView3)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView4)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView5)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView6)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView7)).setImageResource(0);
          ((ImageView)findViewById(R.id.imageView8)).setImageResource(0);
  
          TextView status = findViewById(R.id.status);
          status.setText(&quot;X's Turn - Tap to play&quot;);
  
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  }
  </code></pre>
  <p>The app crashes usually after one input although sometimes crashes after one whole game is completed
  Unable to understand as the on-click function works fine.</p>