java - 渲染时出现异常 : com. android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup

标签 java android eclipse grid-layout

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

渲染期间出现异常:com.android.layoutlib.bridge.MockView 无法转换为 android.view.ViewGroup 异常详细信息记录在 Window > Show View > Error Log 中 无法实例化以下类: - 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,但仍然出现同样的错误。

最佳答案

尝试使用下面的代码(我修改了你的代码,没有添加任何其他东西):

<?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/

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

最好的问候

关于java - 渲染时出现异常 : com. android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21459891/

相关文章:

java - JFrame去除任务栏图标

Java对象数组

android - ContentProvider线程

java - 录音并 POST 到服务器

Java断言打印

java - 如何使用 ZK 在标签 div 中加载数据

java - 计算存款单的利息

java - 如何为 Java Eclipse 项目配置 Travis CI 构建测试?

android - Retrofit 2.0 - 自定义 CallAdapterFactory - 回调不会在 MainThread 上发生

android - findviewbyid() 在 Eclipse 中的自定义适配器中返回 null