如何设置GridLayout变量?

时间:2019-05-07 12:20:47

标签: java android

我试图对GridLayout及其子视图执行一些操作,但出现错误。 请帮助我是Android和Java开发的新手。

我正在使用的代码

 GridLayout grid = (GridLayout) findViewById(R.id.grid);

        for(int i = 0 ; i <grid.getChildCount(); i++){

            ImageView counter = (ImageView) grid.getChildAt(i);

            counter.setImageDrawable(null);

        }


我遇到错误

05-07 17:35:00.064 17330-17330/com.example.adityarana.gameconnect3 
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.adityarana.gameconnect3, PID: 17330
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at ...        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
        at android.view.View.performClick(View.java:6392) 
        at android.view.View$PerformClick.run(View.java:25133) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:198) 
        at android.app.ActivityThread.main(ActivityThread.java:7038) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836) 
     Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout
        at com.example.adityarana.gameconnect3.MainActivity.restart(MainActivity.java:100)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
        at android.view.View.performClick(View.java:6392) 
        at android.view.View$PerformClick.run(View.java:25133) 
        at android.os.Handler.handleCallback(Handler.java:790) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:198) 
        at android.app.ActivityThread.main(ActivityThread.java:7038) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:523) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:836) 
05-07 17:35:00.092 17330-17330/com.example.adityarana.gameconnect3 

0 个答案:

没有答案