遍历GridLayout子项以隐藏3x3电路板的ImageViews

时间:2018-10-17 19:30:19

标签: java android

我一直试图隐藏GridLayout 3x3板的所有图像视图。我尝试使用循环遍历布局和imageView.setImageDrawable(null);的所有子项;

注意:以下代码已放置在playAgain(视图视图)按钮方法中。

public void playAgain(View view){

        Button playAgainButton = (Button) findViewById(R.id.playAgainButtonID);
        TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
        playAgainButton.setVisibility(View.INVISIBLE);


        winnerTextView.animate().translationYBy(-100);

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

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

           ImageView imageView = (ImageView) gridLayout.getChildAt(i);
           imageView.setBackgroundResource(0);
       }

after pressing the button the app shows this error

这是logcat的日志:

  

E / Android运行时:致命异常:主要           流程:com.rootcomp.bills.joincoin,PID:10140           java.lang.IllegalStateException:无法执行android:onClick的方法               在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)               在android.view.View.performClick(View.java:6256)               在android.view.View $ PerformClick.run(View.java:24701)               在android.os.Handler.handleCallback(Handler.java:789)               在android.os.Handler.dispatchMessage(Handler.java:98)               在android.os.Looper.loop(Looper.java:164)               在android.app.ActivityThread.main(ActivityThread.java:6541)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)            造成原因:java.lang.reflect.InvocationTargetException               在java.lang.reflect.Method.invoke(本机方法)               在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)               在android.view.View.performClick(View.java:6256)               在android.view.View $ PerformClick.run(View.java:24701)               在android.os.Handler.handleCallback(Handler.java:789)               在android.os.Handler.dispatchMessage(Handler.java:98)               在android.os.Looper.loop(Looper.java:164)               在android.app.ActivityThread.main(ActivityThread.java:6541)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)            造成原因:java.lang.ClassCastException:android.support.v7.widget.GridLayout无法强制转换为       android.widget.GridLayout               在com.rootcomp.bills.joincoin.MainActivity.playAgain(MainActivity.java:99)               在java.lang.reflect.Method.invoke(本机方法)               在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)               在android.view.View.performClick(View.java:6256)               在android.view.View $ PerformClick.run(View.java:24701)               在android.os.Handler.handleCallback(Handler.java:789)               在android.os.Handler.dispatchMessage(Handler.java:98)               在android.os.Looper.loop(Looper.java:164)               在android.app.ActivityThread.main(ActivityThread.java:6541)               在java.lang.reflect.Method.invoke(本机方法)               在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)       2018-10-18 01:48:21.514 1740-5247 / system_process W / ActivityManager:       强制完成活动com.rootcomp.bills.joincoin / .MainActivity

1 个答案:

答案 0 :(得分:1)

您似乎投放了错误版本的GridLayout。

  

java.lang.ClassCastException:android.support.v7.widget.GridLayout无法在com.rootcomp.bills.joincoin.MainActivity.playAgain(MainActivity.java:99)上转换为android.widget.GridLayout。

您在xml中使用支持版本,在代码中使用非支持版本吗?

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

它们都被称为GridLayout,但是来自不同的库。