what should i do to run this play again button, app crashes when its pressed(making a tic tac toe game)?

时光怂恿深爱的人放手 提交于 2020-01-22 03:31:25

问题


plz help me out in this problem i tried many changes that were suggested in android studio basically the problem occurs in playAgain function it crashes but it shold clear the images im the imageviews

main activity

    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);
        }
    }

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.GridLayout at com.example.xo.MainActivity.playAgain(MainActivity.java:96) 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)  2019-10-18 23:32:30.438 13338-13338/com.example.xo I/Process: Sending signal. PID: 13338 SIG:**


回答1:


GridLayout you are using in xml is different from gridlayout you import in your java class. Both must be same. The error is java.lang.ClassCastException and the reason for error is in your xml you are using androidx gridlayout and in your java class, you are using android gridlayout.



来源:https://stackoverflow.com/questions/58456382/what-should-i-do-to-run-this-play-again-button-app-crashes-when-its-pressedmak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!