java.lang.IllegalStateException当我尝试按标识

时间:2017-02-12 22:54:24

标签: java android illegalstateexception

我遇到了以下错误:

FATAL EXCEPTION: main
    Process: com.example.android.connectthree, PID: 12307
    java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:5697)
    at android.widget.TextView.performClick(TextView.java:10814)
    at android.view.View$PerformClick.run(View.java:22526)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7229)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:5697) 
    at android.widget.TextView.performClick(TextView.java:10814) 
    at android.view.View$PerformClick.run(View.java:22526) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7229) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
    Caused by: java.lang.ClassCastException: android.widget.GridLayout cannot be cast to android.support.v7.widget.GridLayout
    at com.example.android.connectthree.MainActivity.playAgainClicked(MainActivity.java:74)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:5697) 
    at android.widget.TextView.performClick(TextView.java:10814) 
    at android.view.View$PerformClick.run(View.java:22526) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:158) 
    at android.app.ActivityThread.main(ActivityThread.java:7229) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

这是导致错误的方法

public void playAgainClicked(View view) {
    LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
    layout.setVisibility(View.INVISIBLE);

    activePlayer = 0;
    for (int i = 0; i < gameState.length; i++) {
        gameState[i] = 2;
    }
    GridLayout gridLayout = (GridLayout) findViewById(R.id.mainGrid);
    for (int i = 0; i < gridLayout.getChildCount(); i++) {
        ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
    }
}

Gist with a full java file

我试图仔细检查我能做的一切,但仍然没有。

调试后我发现这行代码确实会导致错误。

GridLayout gridLayout = (GridLayout) findViewById(R.id.mainGrid);

1 个答案:

答案 0 :(得分:0)

GridLayoutXML中的

Activity不同。 您使用android.widget.GridLayoutandroid.support.v7.widget.GridLayout并尝试投射它们。