当我在手机上启动APK时,到目前为止一切正常。只有当游戏结束并且我从头开始游戏时,Logcat才会始终出现错误消息,并且应用程序结束。
此代码通过onClick按钮链接,所以我认为应该在某处出现错误
public void playAgain(View view) {
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
android.widget.GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i=0; i<gridLayout.getChildCount(); i++) {
ImageView counter = (ImageView) gridLayout.getChildAt(i);
counter.setImageDrawable(null);
}
for (int i=0; i<gameState.length; i++) {
gameState[i] = 2;
}
activePlayer = 0;
gameActive = true;
}
它会关闭而不是重新启动,并显示以下错误消息:
I/Tag: 6
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: v.example.a3gewinn, PID: 9070
java.lang.IllegalStateException: Could not execute method for
android:onClick
at
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(A
ppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6615)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3500(View.java:784)
at android.view.View$PerformClick.run(View.java:26047)
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:6854)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:4
93)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:860)
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:6615)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3500(View.java:784)
at android.view.View$PerformClick.run(View.java:26047)
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:6854)
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:860)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)'
on a null object reference
at v.example.a3gewinn.activity_game.dropIn(activity_game.java:76)
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:6615)
at android.view.View.performClickInternal(View.java:6587)
at android.view.View.access$3500(View.java:784)
at android.view.View$PerformClick.run(View.java:26047)
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:6854)
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:860)
I/Process: Sending signal. PID: 9070 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport:
'socket'