【问题标题】:MockView cannot be cast to android.view.ViewGroup in xmlMockView 无法在 xml 中强制转换为 android.view.ViewGroup
【发布时间】:2016-06-11 12:11:06
【问题描述】:

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

渲染期间引发的异常: com.android.layoutlib.bridge.MockView 无法转换为 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>

【问题讨论】:

  • 您是否重新启动了 IDE?
  • 是的。但错误仍然相同..
  • 是否可以在 DrawerLayout 中添加两个以上的子视图? @Alif

标签: android android-xml


【解决方案1】:

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

在抽屉布局中,如果我放置一个两个直接布局解决了这个问题。这就是为什么我使用框架布局作为抽屉布局下方的父布局,然后最后添加一个列表视图来显示导航抽屉。

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>

【讨论】:

    【解决方案2】:

    我在我的项目中发现了这个问题,然后我将 support.v4.jar 文件添加到我的项目中,然后它解决了我的问题,所以我认为你可以试试这个。

    【讨论】:

    • 我很久以前就找到了答案并发布了它。也许你的答案会对某人有所帮助:)
    【解决方案3】:

    使用这个

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.sun_soft.app_maker_new.MainActivity"
            tools:ignore="MergeRootFrame" />
    
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#ffffff"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    
        <ListView
            android:id="@+id/right_drawer"
            android:layout_width="@dimen/paanelsize"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="#ffffff"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    
    </android.support.v4.widget.DrawerLayout>

    然后你必须在 mainactivity 中引用列表视图,并且必须为 listitem 制作适配器和膨胀视图。

    我认为在你的代码中你错误地投射了一些观点。 请张贴代码。

    【讨论】:

    • SO 尚不支持 Android XML,您的代码 sn-p 不会在此处执行。
    【解决方案4】:

    我建议您检查布局中引用的资源

    就我而言,我将 psd 文件保存为 png 文件是一个错误。 当我的布局中引用了这个“坏”的 png 文件时,Eclipse 最终会出现这样的错误,即

    Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.widget.ImageView
    

    仅仅因为ImageView中引用的png文件,其实是个psd文件,即:

    android:src="@drawable/my_psd_file_with_wrong_png_extension"
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2023-03-23
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多