【问题标题】:Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup渲染过程中引发异常:com.android.layoutlib.bridge.MockView 无法转换为 android.view.ViewGroup
【发布时间】:2014-01-30 14:59:34
【问题描述】:

我正在 Eclipse 上开发这个 android 项目。我在主要活动上创建了两个 gridLayouts 和一个文本字段。它继续在窗口中显示此警告“无法实例化以下类: - android.support.v7.widget.GridLayout(打开类,显示错误日志)”但是当我尝试向 GridLayout 添加一个按钮时,活动冻结,现在它显示以下消息“注意:此项目包含 Java 编译错误,这可能会导致自定义视图的渲染失败。先修复编译问题。

渲染期间引发异常:com.android.layoutlib.bridge.MockView 无法转换为 android.view.ViewGroup 异常详细信息记录在窗口 > 显示视图 > 错误日志中 无法实例化以下类: - android.support.v7.widget.GridLayout(打开类,显示错误日志)”我的xml文件是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".MainActivity"
    xmlns:app="http://schemas.android.com/apk/res/com.example.simple_calculator">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="94dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <android.support.v7.widget.GridLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="232dp"
        android:layout_marginTop="100dp" >
    </android.support.v7.widget.GridLayout>

    <android.support.v7.widget.GridLayout
        android:layout_width="232dp"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        app:columnCount="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_column="0"
            app:layout_gravity="left|top"
            app:layout_row="0"
            android:text="Button" />

    </android.support.v7.widget.GridLayout>
</FrameLayout>

我已经尝试导入 android-sdk-[platform]/extras/android/support/v7/gridlayout,但还是同样的错误。

【问题讨论】:

    标签: java android eclipse grid-layout


    【解决方案1】:

    尝试使用以下代码(我已经修改了您的代码,没有添加任何其他内容):

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0099cc"
        tools:context=".MainActivity"
        xmlns:app="http://schemas.android.com/apk/res/com.example.simple_calculator">
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="94dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <GridLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="232dp"
            android:layout_marginTop="100dp" >
        </GridLayout>
    
        <GridLayout
            android:layout_width="232dp"
            android:layout_height="match_parent"
            android:layout_marginTop="100dp"
    
           >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_gravity="left"
    
    
                android:text="Button" />
    
        </GridLayout>
    </FrameLayout>
    

    在此代码中,网格布局和 app:column 和重力部分已修改,报告的异常已更正,代码工作正常,布局规则也请查看以下链接:

    http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/
    

    希望这有助于解决问题:)

    最好的问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多