плз, помогите мне в этой проблеме, я перепробовал много изменений, которые были предложены в андроид студии, в основном проблема возникает в функции playAgain, она вылетает, но она очищает изображения в изображениях
основная деятельность
package com.example.xo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
0=o,1=x,2=empty
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 && 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).setDuration(300);
for (int[] winningPosition : winningPositions) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
gameActive = false;
String winner = "";
if (activePlayer == 1) {
winner = "O";
} else {
winner = "X";
}
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText(winner + " Won");
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 gridLayout1 = (GridLayout) findViewById(R.id.gridLayout1);
for (int i = 0; i < gridLayout1.getChildCount(); i++) {
ImageView counter = (ImageView) gridLayout1.getChildAt(i);
counter.setImageResource(0);
}
for (int i=0; i<gameState.length; i++) {
gameState[i] = 2;
}
activePlayer = 0;
gameActive = true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Основная деятельность
<?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:id="@+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/gridLayout1"
android:layout_width="409dp"
android:layout_height="409dp"
android:layout_marginTop="8dp"
android:background="@drawable/b"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:rowCount="3">
<ImageView
android:id="@+id/imageView6"
android:layout_width="137dp"
android:layout_height="143dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="127dp"
android:layout_height="150dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="129dp"
android:layout_height="134dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:scaleType="fitXY"
android:tag="3"
app:layout_column="0"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="117dp"
android:layout_height="127dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:scaleType="fitCenter"
android:tag="1"
app:layout_column="1"
app:layout_row="0" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="132dp"
android:layout_height="129dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="140dp"
android:layout_height="122dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="130dp"
android:layout_height="131dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="137dp"
android:layout_height="125dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="129dp"
android:layout_height="127dp"
android:contentDescription="@string/todo"
android:onClick="dropIn"
android:scaleType="centerInside"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
</androidx.gridlayout.widget.GridLayout>
<Button
android:id="@+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="276dp"
android:layout_marginLeft="276dp"
android:layout_marginBottom="56dp"
android:onClick="playAgain"
android:text="@string/play_again"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/winnerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginLeft="36dp"
android:layout_marginBottom="56dp"
android:text="@string/textview"
android:textSize="30sp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
**>>> Logcat
2019-10-18 23:32:30.422 13338-13338/com.example.xo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.xo, PID: 13338
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:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
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:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to
android.widget. (AppCompatViewInflater.java:385) на android.view.View.performClick (View.java:7125) на android.view.View.performClickInternal (View.java:7102) на android.view.View.access $ 3500 (View.java): 801) на android.view.View $ PerformClick.run (View.java:27336) на android.os.Handler.handleCallback (Handler.java:883) на android.os.Handler.dispatchMessage (Handler.java:100)на android.os.Looper.loop (Looper.java:214) на android.app.ActivityThread.main (ActivityThread.java:7356) в java.lang.reflect.Method.invoke (собственный метод) в com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:492) в com.android. internal.os.ZygoteInit.main (ZygoteInit.java:930) 2019-10-18 23: 32: 30.438 13338-13338 / com.example.xo I / Процесс: отправка сигнала. PID: 13338 SIG: **