java – Android应用程序在单击按钮时崩溃
作者:互联网
我正在使用Java在ANDROID STUDIO 3.1.3中制作TIC TAC TOE游戏.
因此,当我点击按钮时,应用程序崩溃了.我无法找到发生这种情况的原因.请帮忙.
按钮代码如下:
public void playAgain(View view){
gameIsActive = true;
activePlayer = 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);
}
}
按钮的XML代码如下:
<Button
android:id="@+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="185dp"
android:onClick="playAgain"
android:text="play again"/>
这是我的第一个问题所以请不要介意我提出问题的不成熟的方法.
Logcat代码段是:
09-13 18:54:34.720 16114-16114/? I/nihal.tictacto: Not late-enabling -Xcheck:jni (already on)
09-13 18:54:34.777 16114-16114/? W/nihal.tictacto: Unexpected CPU variant for X86 using defaults: x86
09-13 18:54:35.197 16114-16114/com.nihal.tictactoe W/nihal.tictacto: JIT profile information will not be recorded: profile file does not exits.
09-13 18:54:35.201 16114-16114/com.nihal.tictactoe I/chatty: uid=10088(com.nihal.tictactoe) identical 10 lines
09-13 18:54:35.202 16114-16114/com.nihal.tictactoe W/nihal.tictacto: JIT profile information will not be recorded: profile file does not exits.
09-13 18:54:35.269 16114-16114/com.nihal.tictactoe I/InstantRun: starting instant run server: is main process
09-13 18:54:35.804 16114-16114/com.nihal.tictactoe W/nihal.tictacto: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
09-13 18:54:35.805 16114-16114/com.nihal.tictactoe W/nihal.tictacto: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
09-13 18:54:36.177 16114-16114/com.nihal.tictactoe D/OpenGLRenderer: Skia GL Pipeline
09-13 18:54:36.254 16114-16114/com.nihal.tictactoe D/android.support.v7.widget.GridLayout: horizontal constraints: x1-x0>=450, x2-x1>=495, x3-x2>=510, x3-x0<=1440 are inconsistent; permanently removing: x3-x0<=1440.
09-13 18:54:36.255 16114-16114/com.nihal.tictactoe D/android.support.v7.widget.GridLayout: vertical constraints: y3-y0>=2176, y3-y2<=750, y2-y1<=720, y1-y0<=570 are inconsistent; permanently removing: y3-y2<=750.
09-13 18:54:36.326 16114-16137/com.nihal.tictactoe I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
09-13 18:54:36.327 16114-16137/com.nihal.tictactoe I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
09-13 18:54:36.327 16114-16137/com.nihal.tictactoe I/OpenGLRenderer: Initialized EGL, version 1.4
09-13 18:54:36.327 16114-16137/com.nihal.tictactoe D/OpenGLRenderer: Swap behavior 1
09-13 18:54:36.327 16114-16137/com.nihal.tictactoe W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-13 18:54:36.327 16114-16137/com.nihal.tictactoe D/OpenGLRenderer: Swap behavior 0
09-13 18:54:36.337 16114-16137/com.nihal.tictactoe D/EGL_emulation: eglCreateContext: 0xce486000: maj 3 min 1 rcv 4
09-13 18:54:36.369 16114-16137/com.nihal.tictactoe D/EGL_emulation: eglMakeCurrent: 0xce486000: ver 3 1 (tinfo 0xe48b1d00)
09-13 18:54:36.379 16114-16137/com.nihal.tictactoe E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
09-13 18:54:36.565 16114-16137/com.nihal.tictactoe D/EGL_emulation: eglMakeCurrent: 0xce486000: ver 3 1 (tinfo 0xe48b1d00)
09-13 18:55:33.056 16114-16114/com.nihal.tictactoe D/AndroidRuntime: Shutting down VM
09-13 18:55:33.077 16114-16114/com.nihal.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nihal.tictactoe, PID: 16114
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:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
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:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
at com.nihal.tictactoe.MainActivity.playAgain(MainActivity.java:115)
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:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
09-13 18:55:33.203 16114-16114/com.nihal.tictactoe I/Process: Sending signal. PID: 16114 SIG: 9
解决方法:
您正在使用错误类型的GridView.从你的logcat:
Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout
cannot be cast to android.widget.GridLayout
因此,在Java文件的顶部,您应该看到如下所示的行:
import android.widget.GridLayout;
替换为:
import android.support.v7.widget.GridLayout;
标签:java,android,tic-tac-toe 来源: https://codeday.me/bug/20190627/1303619.html