渲染过程中引发的异常:com.android.layoutlib.bridge.MockView不能转换为android.view.ViewGroup。

3
我正在使用Eclipse开发这个Android项目。我在主活动中创建了两个GridLayout和一个文本字段。它一直在窗口中显示这个警告:“以下类无法实例化: - 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-[平台]/extras/android/support/v7/gridlayout,但仍然出现相同的错误。

1个回答

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以及gravity部分进行了修改,并且已经纠正了报告的异常,代码运行良好。对于布局规则,请查看以下链接:
http://www.curious-creature.org/2009/02/22/android-layout-tricks-1/

希望这能帮助解决问题 :)
最好的问候

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接