其他分享
首页 > 其他分享> > 无法将MockView强制转换为xml中的android.view.ViewGroup

无法将MockView强制转换为xml中的android.view.ViewGroup

作者:互联网

在检查图形布局时,出现此错误:

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

下面是我发布的activity_main.xml代码:

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>

解决方法:

发生此错误是由于抽屉布局的错误实现.
我将三个直接布局放置到抽屉布局中会发生错误.根据Google文档,它直接采用了两种布局.

在“抽屉布局”中,如果放置两个直接布局,则可以解决此问题.这就是为什么我将“框架布局”用作“抽屉布局”下面的父布局,然后最终添加“列表视图”以显示导航抽屉的原因.

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>

标签:android-xml,android
来源: https://codeday.me/bug/20191029/1956581.html