英文:
App crashing after clicking Button- java.lang.IllegalStateException: Could not execute method for android:onClick
问题
错误
E/AndroidRuntime: 致命异常:主要
进程:com.example.connect3,PID:10838
java.lang.IllegalStateException:无法执行android:onClick的方法
在androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)中
在android.view.View.performClick(View.java:6256)
在android.view.View$PerformClick.run(View.java:24701)
在android.os.Handler.handleCallback(Handler.java:789)
在android.os.Handler.dispatchMessage(Handler.java:98)
在android.os.Looper.loop(Looper.java:164)
在android.app.ActivityThread.main(ActivityThread.java:6541)
在java.lang.reflect.Method.invoke(Native Method)
在com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
由:java.lang.reflect.InvocationTargetException
在java.lang.reflect.Method.invoke(Native Method)
在androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)中
在android.view.View.performClick(View.java:6256)中
在android.view.View$PerformClick.run(View.java:24701)中
在android.os.Handler.handleCallback(Handler.java:789)中
在android.os.Handler.dispatchMessage(Handler.java:98)中
在android.os.Looper.loop(Looper.java:164)中
在android.app.ActivityThread.main(ActivityThread.java:6541)中
在java.lang.reflect.Method.invoke(Native Method)中
在com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)中
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)中
由:java.lang.ClassCastException:androidx.gridlayout.widget.GridLayout无法转换为android.widget.GridLayout
在com.example.connect3.MainActivity.playAgain(MainActivity.java:82)中
在java.lang.reflect.Method.invoke(Native Method)中
在androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)中
在android.view.View.performClick(View.java:6256)中
在android.view.View$PerformClick.run(View.java:24701)中
在android.os.Handler.handleCallback(Handler.java:789)中
在android.os.Handler.dispatchMessage(Handler.java:98)中
在android.os.Looper.loop(Looper.java:164)中
在android.app.ActivityThread.main(ActivityThread.java:6541)中
在java.lang.reflect.Method.invoke(Native Method)中
在com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)中
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)中
从目标VM断开连接,地址:'localhost:8600',传输:'socket'
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
英文:
So, I am working on a app similar to 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
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
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
答案1
得分: 1
错误通过将以下代码:
import android.widget.gridLayout;
更改为
import androidx.gridlayout.widget.GridLayout;
得以解决。
英文:
The error was solved by changing
import android.widget.gridLayout;
to
import androidx.gridlayout.widget.GridLayout;
答案2
得分: 0
你没有分享你的 .xml 文件,但我认为你的 onClick 方法在那里缺失了,你需要添加它。
你需要在 Button 的 .xml 文件中添加一个像这样的 onClick 方法:
<Button
<!-- 其他内容在这里 -->
android:onClick="buttonClick"/>
然后你需要在你的 .java Activity 中调用这个 onClick 方法,像这样:
public void buttonClick(View v){
// 在这里处理相关操作
}
英文:
You didn't share your .xml file, but I think your onClick method is missing there, you need to add it.
You need to give your Button an onClick Method in its .xml file like this:
<Button
<!-- other stuff here -->
android:onClick="buttonClick"/>
Than you have to call this onClick method in your .java Activity like this:
public void buttonClick(View v){
//do the stuff here
}
答案3
得分: 0
Sure, here's the translated code:
public class MainActivity extends AppCompatActivity {
// 0=黄色, 1=红色;
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}}; // 获胜位置.
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("尚未产生胜者");
text.setVisibility(View.VISIBLE);
press.setVisibility(View.VISIBLE);
} else {
count = 0;
}
ImageView counter = (ImageView) x;
int tapCounter = Integer.parseInt(counter.getTag().toString()); // 从图像获取点击值.
if (gameState[tapCounter] == 2 && gameOn) {
counter.setTranslationY(-1500); // 图像消失
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 = "红色";
} else {
winner = "黄色";
}
text.setText(winner + "\t获胜!");
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);
}
}
Please note that variable and method names have been translated, and the comments have been translated as well. However, some comments that involve HTML entities or special characters might not display correctly in this plain text format. If you encounter any issues with the translated comments, you might need to manually adjust them after pasting the code into your environment.
英文:
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.
https://github.com/Jarvis-byte/3T_Application.git
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论