androidx.gridlayout.widget.GridLayout cannot be cast to android.widget.GridLayout

痞子三分冷 提交于 2019-12-25 18:35:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!