Jashan Ubhi:
私は、Androidに新しいです。私は、Android Studioでの単純な三目並べゲームを作成しました。これは、その、それを回しているとも受賞している告げるグリッドとステータスバーがあります。私はまた、ゲームをリセットする方法が追加されました。ゲームが正常に動作しますが、プレイヤーがゲームのクラッシュを獲得するたびに(TicTacToemeが停止しました)。
私はまた、いくつかの変数を初期化しようとしたが、それは私のために働いていませんでした。
ここに私の完全なMainActivity.javaコードは次のとおりです。
package com.jashanubhi.tictactoeme;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int activePlayer = 0;
boolean gameActive = true;
int[] gameState = {2,2,2,2,2,2,2,2,2};
int[][] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8},
{0,4,8}, {2,4,6},
{0,3,6}, {1,4,7}, {2,5,8}};
public void playerTap(View view) {
ImageView img;
img = (ImageView) view;
int tappedImage;
tappedImage = Integer.parseInt(img.getTag().toString());
TextView status;
status = findViewById(R.id.status);
if (gameState[tappedImage] == 2 && gameActive) {
gameState[tappedImage] = activePlayer;
img.setTranslationY(-1000f);
if (activePlayer == 0) {
img.setImageResource(R.drawable.x);
activePlayer = 1;
status.setText("O's Turn - Tap to Play");
} else {
img.setImageResource(R.drawable.o);
activePlayer = 0;
status.setText("X's Turn - Tap to Play");
}
img.animate().translationYBy(1000f).setDuration(300);
}
for (int[] winningPosition : winningPositions) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {
if (winningPosition[0] == 0) {
status.setText("X has won");
}
if (winningPosition[0] == 1) {
status.setText("O has won");
}
gameActive = false;
}
}
if(!gameActive){
gameReset(view);
}
}
public void gameReset(View view){
activePlayer = 0;
gameActive = true;
for(int i = 0; i <= gameState.length; i++){
gameState[i] = 2;
}
((ImageView)findViewById(R.id.imageView0)).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);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ここに私のLogcat(唯一の誤差)は、次のとおりです。
2020-03-29 15:15:36.635 6517-6517/com.jashanubhi.tictactoeme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jashanubhi.tictactoeme, PID: 6517
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
at com.jashanubhi.tictactoeme.MainActivity.gameReset(MainActivity.java:61)
at com.jashanubhi.tictactoeme.MainActivity.playerTap(MainActivity.java:53)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
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:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
ここに私のXMLは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/title"
android:textSize="38dp"
android:textStyle="bold"
app:fontFamily="sans-serif-light"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="398dp"
android:layout_height="398dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/grid" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X's Turn - Tap to Play"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<LinearLayout
android:layout_width="398dp"
android:layout_height="398dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/textView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView0"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="0" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView3"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="3" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="4" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView6"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="6" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="7" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="playerTap"
android:padding="23sp"
android:tag="8" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
漢RAZ:
あなたの問題がであるgameReset
あなたが取得している機能java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
なぜならそこに内部ループの。
あなたがやっているfor(int i = 0; i <= gameState.length; i++)
が、停止するループのためのあなたの用語があるi<=gameState.length
代わりにi<gameState.length
、配列の長さが5であり、あなたはあなたがアクセスできる最大の場所である第五を取得したい場合は、例えば、配列の長さのPOSで配列にアクセスするには、この原因あなたがすべき配列arr[4]
がありますが、アクセスしようとしているarr[5]
配列のため、あなたが取得しているではありませんjava.lang.ArrayIndexOutOfBoundsException
。