所以,我正在开发一个类似于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公司
3条答案
按热度按时间nfs0ujit1#
这个错误是通过改变
import android.widget.gridLayout;
至import androidx.gridlayout.widget.GridLayout;
qc6wkl3g2#
你可以从这里查我的井字游戏代码。https://github.com/jarvis-byte/3t_application.git
mo49yndu3#
您没有共享您的.xml文件,但我认为您的onclick方法丢失了,您需要添加它。您需要在按钮的.xml文件中为其指定onclick方法,如下所示:
然后在.java活动中调用onclick方法,如下所示: