Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View can not be cast in to ViewGroup in android layout

I am designing a layout for android application. Here is the skeleton of the layout. enter image description here

What I tried is that use a table layout since GUI can be broken in to table rows and columns. Basically, I wanted to break the layout in to two columns as you see. Here is the xml code

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 2 columns -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <View
            android:id="@+id/view1"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </View>

        <View
            android:id="@+id/view2"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content" >
            <Button
                android:id="@+id/button1"
                android:text="Column 2" />
        </View>

    </TableRow>


</TableLayout>

I get following error in the GUI

Exception raised during rendering: android.view.View cannot be cast to android.view.ViewGroup

I think this says we can not group views. If it is not possible, how to deal with this. I am from web background. I thought I can use view as a div like in html.

like image 999
newday Avatar asked Feb 17 '23 12:02

newday


1 Answers

You can group Views, but you need to use a ViewGroup not a View.

Examples of ViewGroups are LinearLayout (for displaying sub-Views horizontally or vertically), RelativeLayout (for displaying sub-Views using relative positions), FrameLayout, TableLayout etc. Use whichever ViewGroup suits your needs.

To get something working, change the two Views in your XML to FrameLayouts. And also give the Button and TextView layout parameters (e.g. android:layout_width="wrap_content", same for height) - otherwise you'll get another error when you run it.

like image 181
nmw Avatar answered Mar 02 '23 22:03

nmw