Java 渲染过程中引发异常:com.android.layoutlib.bridge.MockView 无法转换为 android.view.ViewGroup

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21459891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:49:23  来源:igfitidea点击:

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

javaandroideclipsegrid-layout

提问by Abdul Jabbar

I'm working on this android project on eclipse. I created two gridLayouts and a text field on main activity. It keeps on showing this warning in a window "The following classes could not be instantiated: - android.support.v7.widget.GridLayout (Open Class, Show Error Log)" but as i try to add a button to the GridLayout, the activity freezes and now it shows following message "NOTE: This project contains Java compilation errors, which can cause rendering failures for custom views. Fix compilation problems first.

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

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup Exception details are logged in Window > Show View > Error Log The following classes could not be instantiated: - android.support.v7.widget.GridLayout (Open Class, Show Error Log)" My xml file is:

渲染过程中引发异常: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>

I already tried importing android-sdk-[platform]/extras/android/support/v7/gridlayout, but still same error.

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

回答by Sarabjeet Singh

Try using following code(I have modified your code, not added any other stuff) :

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

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

In this code Grid Layout and the app:column and gravity parts are modified and the exception reported was corrected and code works fine and also for the layout rules please check the following link:

在此代码中网格布局和 app:column 和重力部分被修改,报告的异常得到纠正,代码工作正常,对于布局规则,请检查以下链接:

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

Hope this helps in resolving problem :)

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

Best Regards

此致