java.lang.illegalstateexception:无法执行的方法android:onclick

8ljdwjyq  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(483)

所以,我正在开发一个类似于tictactoe的应用程序。我已经分配了一个按钮,点击它会再次启动游戏。但每当我点击它 java.lang.IllegalStateException: Could not execute method for android:onClick 错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.connect3, PID: 10838
    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:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     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:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
     Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout
        at com.example.connect3.MainActivity.playAgain(MainActivity.java:82)
        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:6256) 
        at android.view.View$PerformClick.run(View.java:24701) 
        at android.os.Handler.handleCallback(Handler.java:789) 
        at android.os.Handler.dispatchMessage(Handler.java:98) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

主活动.java

package com.example.connect3;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    int aplayer=0;
    //0=zero, 1= cross

    int[] gameState = {2,2,2,2,2,2,2,2,2};

    int[][] winingPositions = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};

    @SuppressLint("SetTextI18n")
    public void dropIn (View view)
    {
        ImageView counter = (ImageView) view;

        int tappedCounter = Integer.parseInt(counter.getTag().toString());

        if (gameState[tappedCounter] == 2) {

            gameState[tappedCounter] = aplayer;

            counter.setTranslationY(-1000f);

            if (aplayer == 0) {
                counter.setImageResource(R.drawable.zero);
                aplayer = 1;
            } else {
                counter.setImageResource(R.drawable.cross);
                aplayer = 0;
            }

            counter.animate().translationYBy(1000f).setDuration(300);

            for (int[] winingPosition : winingPositions)
            {
                if (gameState[winingPosition[0]] == gameState[winingPosition[1]] && gameState[winingPosition[1]] == gameState[winingPosition[2]] && gameState[winingPosition[0]] != 2)
                {
                    String winner= " Cross";
                    if (gameState[winingPosition[0]] == 0)
                    {
                     winner="Zero";
                    }

                    //someone has won

                    TextView winMsg = (TextView) findViewById(R.id.winMsg);
                    winMsg.setText(winner + " has WON!!");

                    LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainlayout);
                    layout.setVisibility(View.VISIBLE);
                }

            }
        }
    }

    public void playAgain(View view)
    {

        LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainlayout);
        layout.setVisibility((View.INVISIBLE));

        aplayer=0;
        for (int i = 0; i < gameState.length; i++) {

            gameState[i] = 2;

        }

        GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

        for (int i = 0; i< gridLayout.getChildCount(); i++) {

            ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

logcat公司

nfs0ujit

nfs0ujit1#

这个错误是通过改变 import android.widget.gridLayout;import androidx.gridlayout.widget.GridLayout;

qc6wkl3g

qc6wkl3g2#

public class MainActivity extends AppCompatActivity {
    //0=Yellow,1=Red,;
    int activePlayer = 0;
    int gameState[] = {2, 2, 2, 2, 2, 2, 2, 2, 2};
    int count=0;
    int winingPositions[][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {6, 4, 2}}; // Winning position.
    boolean gameOn = true;

    public void dropIn(View x) {

        Button press = (Button) findViewById(R.id.button);
        TextView text = (TextView) findViewById(R.id.textView2);
        for (int i = 0; i < gameState.length; i++)
        {
            if (gameState[i] == 2)

            {
                count++;
            }
        }
            if(count==1)
            {
                text.setText( "NO ONE HAS WON");
                text.setVisibility(View.VISIBLE);
                press.setVisibility(View.VISIBLE);

            }
            else
            {
                count=0;
            }

                     ImageView counter = (ImageView) x;
                     int tapCounter = Integer.parseInt(counter.getTag().toString());       //geting the tap value from the image.

                     if (gameState[tapCounter] == 2 && gameOn) {
                         counter.setTranslationY(-1500);                                     //vanishing the image

                         gameState[tapCounter] = activePlayer;

                         if (activePlayer == 0) {
                             counter.setImageResource(R.drawable.yellow);
                             counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
                             activePlayer = 1;
                         } else {
                             counter.setImageResource(R.drawable.red);
                             counter.animate().translationYBy(1500).rotation(3600).setDuration(300);
                             activePlayer = 0;
                         }
                         for (int y[] : winingPositions) {
                             if (gameState[y[0]] == gameState[y[1]] && gameState[y[1]] == gameState[y[2]] && gameState[y[0]] != 2)
                             {
                                 gameOn = false;
                                 String winner = "";
                                 if (gameState[y[0]] == 1) {
                                     winner = "RED";
                                 } else {
                                     winner = "YELLOW";
                                 }

                                 text.setText(winner + "\tHAS WON");
                                 text.setVisibility(View.VISIBLE);
                                 press.setVisibility(View.VISIBLE);

                             }

                         }

                     }

                 }

  public void playAgain(View v)
    {
        Button press = (Button) findViewById(R.id.button);
        TextView text = (TextView) findViewById(R.id.textView2);
        text.setVisibility(View.INVISIBLE);
        press.setVisibility(View.INVISIBLE);
        androidx.gridlayout.widget.GridLayout g = (androidx.gridlayout.widget.GridLayout) findViewById(R.id.gridLayout1);

        for (int i = 0; i < g.getChildCount(); i++) {
            ImageView child = (ImageView) g.getChildAt(i);
            child.setImageDrawable(null);

        }
        for(int i=0;i<gameState.length;i++)
        {
            gameState[i]=2;

        }
         activePlayer = 0;

        gameOn = true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

你可以从这里查我的井字游戏代码。https://github.com/jarvis-byte/3t_application.git

mo49yndu

mo49yndu3#

您没有共享您的.xml文件,但我认为您的onclick方法丢失了,您需要添加它。您需要在按钮的.xml文件中为其指定onclick方法,如下所示:

<Button
<!-- other stuff here -->
 android:onClick="buttonClick"/>

然后在.java活动中调用onclick方法,如下所示:

public void buttonClick(View v){

       //do the stuff here
    }

相关问题