当用户单击按钮时,它给出IllegalStateException

时间:2019-08-25 22:49:18

标签: java android android-studio

当我在手机上启动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'

1 个答案:

答案 0 :(得分:0)

通常,IllegalStateException用于表示“某个方法已在非法或不适当的时间被调用

换句话说,Java环境或Java应用程序的状态不适合请求的操作