Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
 }
like image 440
Anil Singh Avatar asked Dec 05 '19 13:12

Anil Singh


2 Answers

Change

import android.widget.gridLayout 

to

import androidx.gridlayout.widget.GridLayout;

and your code will work

like image 60
Avinabh chambial Avatar answered Nov 15 '22 00:11

Avinabh chambial


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);
} 
like image 34
Ziad JD Avatar answered Nov 15 '22 01:11

Ziad JD