简体   繁体   中英

java.lang.IllegalStateException when I try to find GridLayout by id

I've encountered the following error:

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) 

Here's a method which causes an error

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

I have tried to double check everything I can, still nothing.

After debugging I found out that exactly this line of code causes an error.

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

GridLayout in XML and Activity are different. You use android.widget.GridLayout and android.support.v7.widget.GridLayout and try to cast them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM