简体   繁体   English

单击按钮后应用程序崩溃 - java.lang.IllegalStateException:无法执行 android 的方法:onClick

[英]App crashing after clicking Button- java.lang.IllegalStateException: Could not execute method for android:onClick

So, I am working on a app similar to TicTacToe.所以,我正在开发一款类似于 TicTacToe 的应用程序。 I have assigned a button, which when clicked will start the game once again.我已经分配了一个按钮,单击该按钮将再次开始游戏。 But whenever I click that i get java.lang.IllegalStateException: Could not execute method for android:onClick但是每当我点击我得到java.lang.IllegalStateException: Could not execute method for android:onClick

Error错误

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'

MainActivity.java MainActivity.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日志猫

The error was solved by changing import android.widget.gridLayout;通过更改import android.widget.gridLayout; to import androidx.gridlayout.widget.GridLayout; import androidx.gridlayout.widget.GridLayout;

You didn't share your.xml file, but I think your onClick method is missing there, you need to add it.您没有共享您的 .xml 文件,但我认为那里缺少您的 onClick 方法,您需要添加它。 You need to give your Button an onClick Method in its.xml file like this:您需要在 its.xml 文件中为 Button 提供一个 onClick 方法,如下所示:

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

Than you have to call this onClick method in your.java Activity like this:比你必须在你的.java 活动中调用这个 onClick 方法是这样的:

public void buttonClick(View v){

       //do the stuff here
    }
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);
    }
}

You can check my code for Tic Tac Toe from here.您可以从这里查看我的 Tic Tac Toe 代码。 https://github.com/Jarvis-byte/3T_Application.git https://github.com/Jarvis-byte/3T_Application.git

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:无法为 android 执行方法:onClick / ZE84E30B9390CDB67ZDDB6 - java.lang.IllegalStateException: Could not execute method for android:onClick / Android java.lang.IllegalStateException:无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick in android Kotlin-java.lang.IllegalStateException:无法执行android:onClick的方法 - Kotlin - java.lang.IllegalStateException: Could not execute method for android:onClick 错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error: java.lang.IllegalStateException: Could not execute method for android:onClick java.lang.IllegalStateException:尝试单击按钮时无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick when trying to click a button java.lang.IllegalStateException:当我运行应用程序时无法执行 android:onClick 的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick when I run app Android-Java:java.lang.IllegalStateException:无法执行 android 的方法:onClick - Android-Java: java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception - Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception Android Studio 问题; java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Android Studio problem ; java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio 3.1.3 中的错误。 java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Error in Android Studio 3.1.3. java.lang.IllegalStateException: Could not execute method for android:onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM