Search code examples
androidandroid-linearlayoutandroid-cardviewclasscastexception

Android: CardView inside LinearLayout makes app to crash


Given the next xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/llCommunityMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="70dp"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/llCommunity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:gravity="center">

            <androidx.cardview.widget.CardView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/cvFindCollege"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:layout_marginTop="20dp"
                android:layout_margin="10dp"
                app:cardCornerRadius="5dp"
                app:cardElevation="10dp"
                app:cardBackgroundColor="#f5f5f5"
    android:theme="@style/Theme.MaterialComponents.Light">

                <com.testmepracticetool.toeflsatactexamprep.component.view.TMButton
                    android:id="@+id/btnSuggestImprovements"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:theme="@style/Theme.MaterialComponents.Light"
                    android:background="@android:drawable/dialog_holo_light_frame"
                    style="@style/button_text_default"
                    android:text="@string/activitycommunity_button_suggestimprovements" />

            </androidx.cardview.widget.CardView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:layout_marginTop="20dp">

                <com.testmepracticetool.toeflsatactexamprep.component.view.TMButton
                    android:id="@+id/btnFindCollege"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:background="@android:drawable/dialog_holo_light_frame"
                    style="@style/button_text_default"
                    android:text="@string/community_button_findcollege" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/llCommunityKhan"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:layout_marginTop="20dp">

                <com.testmepracticetool.toeflsatactexamprep.component.view.TMButton
                    android:id="@+id/btnKhan"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:layout_gravity="center_horizontal"
                    android:background="@android:drawable/dialog_holo_light_frame"
                    style="@style/button_text_default"
                    android:text="@string/activitykhan_title" />

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

    <include layout="@layout/footermenu_default" />

</RelativeLayout>

footer.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMainMenu"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:orientation="horizontal"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:layout_alignParentBottom="true"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="10dp"
    android:visibility="gone"
    android:paddingTop="10dp"
    android:background="@drawable/layout_border_top">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnTests"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:src="@drawable/button_main_tests" />

        <TextView
            android:id="@+id/tvMainMenuTests"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_tests"
            android:textColor="#a8a8a8"
            android:textSize="11sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnRecords"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:src="@drawable/button_main_records" />

        <TextView
            android:id="@+id/tvMainMenuRecords"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_records"
            android:textColor="#a8a8a8"
            android:textSize="11sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llMainMenuImportExport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal"
        android:visibility="gone">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnImportExport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:visibility="gone"
            android:src="@drawable/button_records_importexport" />

        <TextView
            android:id="@+id/tvMainMenuImportExport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_importexport"
            android:textColor="#a8a8a8"
            android:textSize="11sp"
            android:visibility="gone"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/llMainMenuProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal"
        android:visibility="gone">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:visibility="gone"
            android:src="@drawable/button_records_progress" />

        <TextView
            android:id="@+id/tvMainMenuProgress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_progress"
            android:textColor="#a8a8a8"
            android:textSize="11sp"
            android:visibility="gone"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnCommunity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:src="@drawable/button_main_community" />

        <TextView
            android:id="@+id/tvMainMenuCommunity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_community"
            android:textColor="#a8a8a8"
            android:textSize="11sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight=".20"
        android:gravity="center_horizontal">

        <ImageView
            android:contentDescription="@string/pref_category_animations"
            android:id="@+id/btnUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="3dp"
            android:paddingStart="3dp"
            android:paddingRight="3dp"
            android:paddingEnd="3dp"
            android:src="@drawable/button_main_user" />

        <TextView
            android:id="@+id/tvMainMenuUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingTop="5dp"
            android:text="@string/mainmenu_button_User"
            android:textColor="#a8a8a8"
            android:textSize="11sp" />

    </LinearLayout>

</LinearLayout>

My app crashes as soon as it tries to set the layout with the following exception

android.widget.TableRow$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

What may be wrong?


Solution

  • I checked your Layout with the stuff I have from you. I don't have the footer (not tested) and Button implementation. I replaced your Buttons with the default onces. What I changed; You shouldn't use a ScrollView for such amount of Views and it should be on top as a rootView. I replaced your ScrollView with a NestedScrollView and swapped the places with the root RelativeLayout (changed the parameters also):

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        android:scrollbars="none">
    
        <RelativeLayout
            android:id="@+id/llCommunityMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:paddingBottom="70dp">
    
            <LinearLayout
                android:id="@+id/llCommunity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical">
    
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
    
                    <androidx.cardview.widget.CardView 
                        android:id="@+id/cvFindCollege"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:layout_marginTop="20dp"
                        android:paddingLeft="15dp"
                        android:paddingRight="15dp"
                        android:theme="@style/Theme.MaterialComponents.Light"
                        app:cardBackgroundColor="#f5f5f5"
                        app:cardCornerRadius="5dp"
                        app:cardElevation="10dp">
    
                        <Button
                            android:id="@+id/btnSuggestImprovements"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_horizontal"
                            android:background="@android:drawable/dialog_holo_light_frame"
                            android:text="Button 1"
                            android:textAlignment="center"
                            android:theme="@style/Theme.MaterialComponents.Light" />
    
                    </androidx.cardview.widget.CardView>
                </RelativeLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:paddingLeft="15dp"
                    android:paddingRight="15dp">
    
                    <Button
                        android:id="@+id/btnFindCollege"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:background="@android:drawable/dialog_holo_light_frame"
                        android:text="Button 2"
                        android:textAlignment="center" />
    
                </LinearLayout>
    
                <LinearLayout
                    android:id="@+id/llCommunityKhan"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:paddingLeft="15dp"
                    android:paddingRight="15dp">
    
                    <Button
                        android:id="@+id/btnKhan"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:background="@android:drawable/dialog_holo_light_frame"
                        android:text="Button 3"
                        android:textAlignment="center" />
    
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </androidx.core.widget.NestedScrollView>
    

    Yet, if I run the app everything works fine. My guess is that the NestedScrollView served as a rootView solved the problem. If not there are only 2 things you need to check. Your Button implementation or the footer. Cheers :)

    Result:

    Result