当我尝试单击具有onClick方法的按钮时,应用程序崩溃,错误提示“无法为android:onClick执行方法”。当我在文本输入中添加onClick方法时,也会发生这种情况。
这是我的代码:
public class MainActivity extends AppCompatActivity {
// 0= yellow, 1=Red
int activePlayer=0;
boolean gameIsActive=true;
// 2 means un played
int[] gameState={2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winnigPossitions={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
public void dropIn(View view)
{
final ImageView counter=(ImageView) view;
int tapCounter=Integer.parseInt(counter.getTag().toString());
if (gameState[tapCounter]==2 && gameIsActive)
{
gameState[tapCounter]=activePlayer;
if (activePlayer == 0) {
counter.setImageResource(R.drawable.circle);
counter.animate().scaleY(0.5f).scaleX(0.5f).setDuration(50);
counter.animate().scaleX(6f).scaleY(6f).setDuration(50);
counter.animate().scaleY(0.6f).scaleX(0.6f).setDuration(50);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.cross);
counter.animate().scaleY(0.5f).scaleX(0.5f).setDuration(50);
counter.animate().scaleX(6f).scaleY(6f).setDuration(50);
counter.animate().scaleY(0.6f).scaleX(0.6f).setDuration(50);
activePlayer = 0;
}
for (int[] winnigPossitions:winnigPossitions)
{
if (gameState[winnigPossitions[0]]==gameState[winnigPossitions[1]] &&
gameState[winnigPossitions[1]]==gameState[winnigPossitions[2]] &&
gameState[winnigPossitions[0]]!=2)
{
gameIsActive=false;
LinearLayout layout=findViewById(R.id.linearLayout);
layout.setVisibility(View.VISIBLE);
}
else
{
boolean gameIsOver=true;
for (int counterState : gameState)
{
if (counterState==2)
{
gameIsOver=false;
}
}
if (gameIsOver)
{
TextView txtWon=findViewById(R.id.txtWon);
txtWon.setText("It's a Draw");
LinearLayout layout=findViewById(R.id.linearLayout);
layout.setVisibility(view.VISIBLE);
}
}
}
}
}
public void playAgain(View view)
{
gameIsActive=true;
LinearLayout layout=findViewById(R.id.linearLayout);
layout.setVisibility(View.INVISIBLE);
activePlayer=0;
for (int i=0;i<gameState.length;i++)
{
gameState[i]=2;
}
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);
}
}
点击按钮时,这是完整错误:
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at com.example.akash.tikataktoe.MainActivity.playAgain(MainActivity.java:110)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6199)
at android.widget.TextView.performClick(TextView.java:11090)
at android.view.View$PerformClick.run(View.java:23647)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
答案 0 :(得分:0)
此行:
java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout at com.example.akash.tikataktoe.MainActivity.playAgain(MainActivity.java:110)
指向问题的根源。您正在执行“不良”演员表转换,因此必须确保使用正确的类!