gpt4 book ai didi

java - 迭代 GridLayout 子级以隐藏 3x3 板的 ImageView

转载 作者:行者123 更新时间:2023-12-02 10:41:58 25 4
gpt4 key购买 nike

我一直在尝试隐藏 GridLayout 3x3 板的所有 ImageView 。我尝试使用循环来迭代布局和 imageView.setImageDrawable(null); 的所有子级;

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

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/AndroidRuntime: FATAL EXCEPTION: main Process: com.rootcomp.bills.joincoin, PID: 10140 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24701) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:6256)  at android.view.View$PerformClick.run(View.java:24701)  at android.os.Handler.handleCallback(Handler.java:789)  at android.os.Handler.dispatchMessage(Handler.java:98)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout at com.rootcomp.bills.joincoin.MainActivity.playAgain(MainActivity.java:99) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)  at android.view.View.performClick(View.java:6256)  at android.view.View$PerformClick.run(View.java:24701)  at android.os.Handler.handleCallback(Handler.java:789)  at android.os.Handler.dispatchMessage(Handler.java:98)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  2018-10-18 01:48:21.514 1740-5247/system_process W/ActivityManager: Force finishing activity com.rootcomp.bills.joincoin/.MainActivity

最佳答案

看起来您正在转换错误版本的 GridLayout。

java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout at com.rootcomp.bills.joincoin.MainActivity.playAgain(MainActivity.java:99)

您是否在 xml 中使用支持版本,而在代码中使用非支持版本?

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

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

关于java - 迭代 GridLayout 子级以隐藏 3x3 板的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52862309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com