The app is opening and working fine till I made a click Basically it is a Connect-3 game and on clicking it crashes.
My logcat is
12-13 14:22:32.393 2482-2482/com.govinddixit.connect3 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at com.govinddixit.connect3.MainActivity.dropIn(MainActivity.java:44)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method) 12-13 14:24:17.457 2482-2482/com.govinddixit.connect3 I/Process: Sending signal. PID: 2482 SI
G: 9
My java activity is
package com.govinddixit.connect3;
import android.media.Image;
import android.view.MenuItem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.security.KeyStore;
public class MainActivity extends AppCompatActivity
{
//0=yellow,1=red
int activePlayer = 0;
boolean gameisActive = true;
//2 means unplayed
int[] gameState = {2,2,2,2,2,2,2,2};
int[][] winningPosition = {{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 dropIn(View view) {
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2 && gameisActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(36).setDuration(300);
for (int[] winningPosition : winningPosition) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2) {
//someone has won//
gameisActive = false;
String winner = "RED";
if (gameState[winningPosition[0]] == 0) {
winner = "YELLOW";
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText("has won");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(view.VISIBLE);
} else {
boolean gameIsOver = true;
for (int counterState : gameState) {
if (counterState == 2) {
gameIsOver = false;
}
}
if (gameIsOver) {
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText("It's a draw");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(view.VISIBLE);
}
}
}
}
}
}
public void playAgain(View view)
{
gameisActive = true;
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
activePlayer= 0;
for(int i=0;i {
gameState[i]=2;
}
GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
for(int i=0;i {
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
My xml file is
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.govinddixit.connect3.MainActivity">
android:id="@+id/playAgainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:alpha="1"
android:background="?android:attr/colorActivatedHighlight"
android:orientation="vertical"
android:paddingHorizontal="30dp"
android:paddingVertical="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:id="@+id/winnerMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30sp" />
android:id="@+id/playAgainButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="playAgain"
android:text="PLAY AGAIN" />
android:id="@+id/gridLayout"
android:layout_width="360dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/board"
android:columnCount="3"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.496">
android:id="@+id/imageView2"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="35dp"
android:layout_marginTop="35dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView3"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_margin="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView4"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView6"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="35dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="0"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="149dp" />
android:id="@+id/imageView7"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView8"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="35dp"
android:layout_marginTop="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView10"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="2"
android:layout_marginLeft="35dp"
android:layout_marginTop="25dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView11"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="35dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="0" />
android:id="@+id/imageView12"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_column="1"
android:layout_marginLeft="25dp"
android:layout_marginTop="25dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="0" />
Answer
Check that part of your code:
for (int[] winningPosition : winningPosition) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2) {
so your are looping through:
int[][] winningPosition = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
then you take the ints in your 3-ints arrays as an index to look up the element in the other array:
int[] gameState = {2,2,2,2,2,2,2,2};
but that array has only 8 elements!
So when you take the '8' from previous winningPosition
array you would do gameState[8]
which looks for the 9th element which not exists --> java.lang.ArrayIndexOutOfBoundsException: length=8; index=8 at MainActivity.dropIn(line 44)
No comments:
Post a Comment