【问题标题】:Android layout crash: Binary XML file line #10 must supply a layout widthAndroid 布局崩溃:二进制 XML 文件第 10 行必须提供布局宽度
【发布时间】:2011-07-13 16:43:33
【问题描述】:

我很难过,我已经推断它在更大布局的这个合并部分中(edit 谢谢,是的第 10 行很明显,第 10 行的 textview 很明显需要宽度和高度,但仅使用代码中存在的文本视图没有崩溃,我已将其粘贴到第 #10 行)

<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/frameLayoutLatest">
<LinearLayout android:id="@+id/subLinLayoutHeader" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <ImageView android:src="@drawable/layouttriangle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></ImageView>
</LinearLayout>
<TableLayout android:id="@+id/subLinLayout" android:layout_below="@id/subLinLayoutHeader" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <TableRow  android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
            <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
                <!-- list view goes here -->
                <TextView android:layout_gravity="center_vertical|center_horizontal" android:text="Dummy text" android:textColor="#ffffff" android:textSize="16dip"></TextView>
            </ScrollView>
        </LinearLayout>
    </TableRow>
    <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content">
                <TextView android:layout_gravity="center_vertical|center_horizontal" android:text="Latest Articles" android:textColor="#ffffff" android:textSize="16dip"></TextView>
                </TableRow>
        </TableLayout>
        <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content">
         <!--  list view goes here -->
        </ScrollView>
        </LinearLayout>

</TableRow>
</TableLayout>

沃尔多在哪里?

【问题讨论】:

    标签: android exception layout resources crash


    【解决方案1】:

    当然在第 10 行 :) 您的代码没有为 TextView 提供 layout_height 或 layout_width。每个 xml 元素都需要两者。

    这是你的代码:

    <TextView
    android:layout_gravity="center_vertical|center_horizontal"
    android:text="Dummy text"
    android:textColor="#ffffff"
    android:textSize="16dip">
    </TextView>
    

    试试这个(或使用 match_parent 而不是 wrap_content)

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:text="Dummy text"
    android:textColor="#ffffff"
    android:textSize="16dip">
    </TextView>
    

    编辑以解释第二个 TextView 的行为

    嗯,我今天学到了一些新东西。 根据 TableRow 文档,

    The children of a TableRow do not need to specify the layout_width
    and layout_height attributes in the XML file. TableRow always enforces
    those values to be respectively MATCH_PARENT and WRAP_CONTENT.
    

    这就是它没有抛出错误的原因。

    【讨论】:

    • 那为什么在文档中使用另一个完全相同的文本视图不会崩溃呢?我已经把那个复制到上面的地方了。如果布局崩溃或 xml 错误报告与所有 xml 错误一致,那将是显而易见的。但事实并非如此。
    • 我认为它会,它只是首先在第 10 行崩溃。您是说如果将 layout_width 和 layout_height 属性添加到第一个 TextView 而不是第二个则一切正常?
    • 这就是我所说的。布局将毫无问题地呈现在实际设备上。没有 XML builder 错误,没有崩溃等。我不认为它关心 textview 上的宽度高度,这就是我难过的原因
    • 那么我真诚地怀疑第二个 TextView 是否曾经显示在屏幕上。如果是这样,它会产生与您之前看到的错误一样的错误。来自布局文档"All view groups include a width and height (layout_width and layout_height), and each view is required to define them"。我想您可以通过编程方式声明它们,但 Android 将拒绝绘制视图,除非您以一种或另一种方式设置宽度和高度。
    • 不知道如何证明给你看,但是注释掉第一个 textview 并运行那段代码。它显示“最新文章”,logcat甚至不显示错误,也许我可以排名。
    【解决方案2】:

    此文件第 10 行的 ScrollView 中的 TextView 需要 layout_height 和 layout_width 属性,因此系统将知道您希望 TextView 的尺寸。如果您不确定从哪里开始,只需从包含它的 ScrollView 中复制这两个属性即可。

    【讨论】:

    • 那么为什么在文档中使用另一个完全相同的文本视图不会崩溃呢?我已经把那个复制到上面的地方了。如果布局崩溃或 xml 错误报告与所有 xml 错误一致,那将是显而易见的。但事实并非如此。
    • 不,我的意思是如果我把那个 textview 拿出来,把另一个也不正确的那个留在那里,这个代码部分就可以工作。你可以自己试试。 Logcat 甚至不显示错误,并且应用程序运行良好。
    • 它在一个表格行中,以不同的方式处理布局。
    【解决方案3】:

    错误信息还不够清楚吗?缺少布局宽度(两个TextView),如果您解决此问题,您的问题可能会得到解决。

    【讨论】:

    • 那么为什么在文档中使用另一个完全相同的文本视图不会崩溃呢?我已经把那个复制到上面的地方了。如果布局崩溃或 xml 错误报告与所有 xml 错误一致,那将是显而易见的。但事实并非如此。
    • 先在你的代码中添加layout:widthlayout:height,如果还是不行再回来。
    • 成功了。我仍然很好奇为什么它忽略了另一个 textview 上的布局错误并且工作和渲染正常
    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多