Ani*_*ngh 4 java android android-activity androidx
我收到错误
"Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout", in mainActivity.java
GridLayout mygridLayout = findViewById(R.id.gridLayout);
for(int i=0; i<mygridLayout.getChildCount(); i++)
{
((ImageView) mygridLayout.getChildAt(i)).setImageResource(0);
}
Run Code Online (Sandbox Code Playgroud)
小智 11
改变
import android.widget.gridLayout
Run Code Online (Sandbox Code Playgroud)
到
import androidx.gridlayout.widget.GridLayout;
Run Code Online (Sandbox Code Playgroud)
你的代码会起作用
小智 5
在代码中使用它会起作用。
androidx.gridlayout.widget.GridLayout mygridLayout = findViewById(R.id.gridLayout);
for(int i=0; i<mygridLayout.getChildCount(); i++){
((ImageView) mygridLayout.getChildAt(i)).setImageResource(0);
}
Run Code Online (Sandbox Code Playgroud)
您在layout.xml 中使用AndroidX 包,并且从源代码中的包GridLayout导入。使用其中之一并使它们保持一致。Gridlayoutandroid.widget