Search code examples
javaandroidandroid-layoutandroid-studio-3.0android-gridlayout

Why is Grid Layout causing android studio app to crash?


I am using android studio 3.1.4

The app crashes while exwcuting the following part of the code:

  GridLayout layer=(GridLayout)findViewById(R.id.gridId); //Crashes at this point itself.

        for(int i=0;i<layer.getChildCount();i++) {
            ((ImageView)layer.getChildAt(i)).setImageResource(0);
        }

Solution

  • The error is you use android.support.v7.widget.GridLayout in the XML file but GridLayout in the code.

    to fix it, change this line

      GridLayout layer=(GridLayout)findViewById(R.id.gridId); //Crashes at this point itself.
    

    to

     android.support.v7.widget.GridLayout layer=(android.support.v7.widget.GridLayout)findViewById(R.id.gridId); //Crashes at this point itself.
    

    or just import android.support.v7.widget.GridLayout