Naruto Naruto - 5 months ago 21
Android Question

MockView cannot be cast to android.view.ViewGroup in xml

While checking in the graphical layout I am getting this error:


Exception raised during rendering:
com.android.layoutlib.bridge.MockView cannot be cast to
android.view.ViewGroup


Below I am posted my activity_main.xml codes:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout

xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textviewHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:fontFamily="fonts/Dosis.otf"
android:text="@string/heading_chapter"
android:textSize="25dp"
android:visibility="gone" />

<ListView
android:id="@+id/listviewChapters"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/imageviewBanner"
android:layout_below="@+id/textviewHeading"
android:layout_marginTop="5dp" >
</ListView>

<ImageView
android:id="@+id/imageviewBanner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/banner"
android:visibility="invisible" />
</RelativeLayout>

<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>

</android.support.v4.widget.DrawerLayout>

Answer

This Error was occurred because of wrong implementation of the drawer layout. I place a three direct layout to Drawer layout the error would be occurring. As per the Google document, It took the two layout directly.

In Drawer Layout, If I place a two direct Layout solved the problem. That's why I am using the Frame Layout as a parent layout below Drawer Layout then finally add a List View to show the navigation drawer.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textviewHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:fontFamily="fonts/Dosis.otf"
                android:text="@string/heading_chapter"
                android:textSize="25sp"
                android:visibility="gone" />

            <ListView
                android:id="@+id/listviewChapters"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/imageviewBanner"
                android:layout_below="@+id/textviewHeading"
                android:layout_marginTop="5dp" >
            </ListView>

            <ImageView
                android:id="@+id/imageviewBanner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/banner"
                android:visibility="invisible" />
        </RelativeLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</android.support.v4.widget.DrawerLayout>
Comments