Android - Gridlayout android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout

后端 未结 7 1621
说谎
说谎 2020-12-01 20:15

I want to create a GridLayout able to run in all the APIs.

The thing is, when i use GridLayout instead of android.support.v7.widget.GridLayo

相关标签:
7条回答
  • 2020-12-01 20:55

    Replace

    import android.widget.GridLayout;
    

    with

    import android.support.v7.widget.GridLayout;
    

    This was my issue.

    0 讨论(0)
  • 2020-12-01 20:58

    Caused by: java.lang.ClassCastException: android.support.v7.widget.GridLayout cannot be cast to android.widget.GridLayout at devector.dom.gridtest.MainActivity.onCreate(MainActivity.java:21)

    => From this line, it's clear that you must be importing and referring to android.widget.GridLayout where as you have taken android.support.v7.widget.GridLayout in XML layout. Use and refer either of any in both layout and class file.

    0 讨论(0)
  • 2020-12-01 20:59

    Just change the format a little bit: androidx.gridlayout.widget.GridLayout newGrid= (androidx.gridlayout.widget.GridLayout) findViewById (R.id.yourGrid);

    0 讨论(0)
  • 2020-12-01 21:00

    I got the same issue. I solved it by using the GridLayout from package android.support.v7.widget.GridLayout, since I was previously using the android.widget.GridLayout package which gave me the error.

    0 讨论(0)
  • 2020-12-01 21:06
    import androidx.gridlayout.widget.GridLayout
    

    Do this in you MainActiviy sheet.

    You should edit the import.android.widget.GridLayout

    as

    import androidx.gridlayout.widget.GridLayout
    
    0 讨论(0)
  • 2020-12-01 21:07

    I had the same issue and I just go to activity_main.xml and replaced

    android.support.v7.widget.GridLayout
    

    to this:

    android.widget.GridLayout
    
    0 讨论(0)
提交回复
热议问题