问题
I am getting error
"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);
}
回答1:
You are using GridLayout from AndroidX package in your layout.xml and you are importing Gridlayout from package android.widget in your source code. Use one or the other and make them consistent.
回答2:
Use this in the code it will work.
androidx.gridlayout.widget.GridLayout mygridLayout = findViewById(R.id.gridLayout);
for(int i=0; i<mygridLayout.getChildCount(); i++){
((ImageView) mygridLayout.getChildAt(i)).setImageResource(0);
}
来源:https://stackoverflow.com/questions/59195798/androidx-gridlayout-widget-gridlayout-cannot-be-cast-to-android-widget-gridlayou