android 点击执行函数,单击按钮后应用程序崩溃-java.lang.IllegalStateException:无法执行Android的方法:OnClick...

所以,我正在做一个类似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'

Mainactivity.javapackage 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值